首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从R中的表中获得特定列的平均成本

如何从R中的表中获得特定列的平均成本
EN

Stack Overflow用户
提问于 2022-05-16 01:55:18
回答 1查看 23关注 0票数 0

我整天都在试图从我的数据的Price列中得到项目的平均成本,但我就是想不出来。我对这件事不熟悉。所以请原谅,如果我没有正确加载所有信息。以下是详细信息:

  1. 导入了CSV文件。
  2. 创建了一个数据集,仅显示我需要数据的列。
  3. 试图找到健身用品的平均价格。

代码语言:javascript
复制
#clean names
clean_names(wearables)

> #clean names
> clean_names(wearables)
# A tibble: 215 × 15
   name        price body_location category company_name company_url company_mapping… company_city
   <chr>       <chr> <chr>         <chr>    <chr>        <chr>       <chr>            <chr>       
 1 "Barska GB… $49.… Wrist         Fitness  Barska       http://www… Pomona, Califor… Pomona      
 2 "Belkin GS… $24.… Arms          Fitness  Belkin       http://www… Playa Vista, Ca… Playa Vista 
 3 "Ekho Fit-… $105… Wrist         Fitness  Ekho         http://www… Dallas, Texas, … Dallas      
 4 "Fitbit Fl… $94.… Wrist         Fitness  Fitbit       http://www… San Francisco, … San Francis…
 5 "Garmin Fo… $249… Wrist         Fitness  Garmin       http://www… Olathe, Kansas,… Olathe      
 6 "Garmin In… $169… Wrist         Fitness  Garmin       http://www… Olathe, Kansas,… Olathe      
 7 "Garmin Vi… $79.… Wrist         Fitness  Garmin       http://www… Olathe, Kansas,… Olathe      
 8 "Garmin Vi… $129… Wrist         Fitness  Garmin       http://www… Olathe, Kansas,… Olathe      
 9 "Jawbone -… $112… Wrist         Fitness  Jawbone      https://ww… San Francisco, … San Francis…
10 "Jawbone U… $52.… Wrist         Fitness  Jawbone      https://ww… San Francisco, … San Francis…
# … with 205 more rows, and 7 more variables: company_u_s_state <chr>, company_country <chr>,
#   source <chr>, link <chr>, duplicates_note_1 <lgl>, id <dbl>, image <chr>


#Filter for Fitness Category
wearables <- Wearables_DFE %>% filter(Category == "Fitness")

#Verify table
view(wearables)

代码语言:javascript
复制
#Show the applicable columns
wearables %>% select(Category, Name, Body.Location, Price)

> #Filter for Fitness Category
> wearables <- Wearables_DFE %>% filter(Category == "Fitness")

> #Verify table. Please see image attached. I do not know how to save a dataset of a table.
> view(wearables)
?

#Find the average price for a wearable fitness item, excluding the NA's.
wearables %>% group_by (Body.Location) %>% drop_na %>% summarize(average_cost = mean(Price))

> #Find the average price for a wearable fitness item, excluding the NA's.
> wearables %>% group_by (Body.Location) %>% drop_na %>% summarize(average_cost = mean(Price))
# A tibble: 0 × 2
# … with 2 variables: Body.Location <chr>, average_cost <dbl>
Warning message:
In mean.default(Price) : argument is not numeric or logical: returning NA
> 
EN

回答 1

Stack Overflow用户

发布于 2022-05-16 02:07:26

首先,您需要将价格列从字符转换为数字。然后按类别分组,用平均值概括。

试试这个:

代码语言:javascript
复制
    library(dplyr)
mean_prices <- wearables %>%
        mutate(price = as.numeric(gsub(x = price, 
                             pattern = "$", 
                             replacement = ""))) %>%
        group_by(category) %>%
        summarise(mean = mean(price))

view(mean_prices)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72253495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档