将最小值、平均值和最大值的列添加到数据框

Adding columns for min, mean, and max to a data frame

我正在尝试 R 并希望做一些看起来相当简单的事情,但我对如何进行有点迷茫。我正在阅读看起来像这样的 CSV:

translation,category,macrocategory,subcategory,1640,1641,1642,1643,1644
almonds,nuts,Flavoring/Other,,,491,,,
apples,Fruit,Fruits and Vegetables,42,,67,,,
Atlantic herring,Fish,Meat,,52,0,9,,
aurochs,Meat,Meat,game,,4,25,5,
bacon,Meat,Meat,pork,,275.87,78,92,0
barley groats,Grain,Grain,5,9,2,14,56,9
beef,Meat,Meat,Beef,,5.25,,,

我想要的是为每种食品添加最大值、最小值和平均值的新列(因此,每个食品的最大值、最小值和平均值)。我已经处理了一些基本的清理工作,但我对如何从那里继续进行有点迷茫。

library(dplyr)
library(tidyr)

df <- read.csv("foods.csv", sep=",", header = T)
food.clean <- data.frame(foodname=df[,1], data.matrix(df[,5:53]))
str(food.clean) # check

food.clean <- food.clean[rowSums(is.na(food.clean)) < 48, ]
summary(food.clean)

我也试过使用matrixStats包,但是运行报错:

library(matrixStats)
food.matrix <- as.matrix(food.clean)
cbind(food.clean, mean=rowMeans(food.matrix), sd=rowSds(food.matrix), max=rowMaxs(food.matrix))

Error in rowMeans(food.matrix) : 'x' must be numeric

有什么指点吗?谢谢!

尚不完全清楚您的样本数据是什么样的(具体来说,header 行中的 1640,1641,1642,1643,1644),但这应该向您展示了一种使用mtcars 示例数据集。如果您可以 dput 您的实际数据,就更容易提供帮助。具体来说,您可以使用 dplyr::rowwise:

library(dplyr)
df <- tbl_df(mtcars)

head(df)
# Source: local data frame [6 x 11]
# 
#    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1


df %>%
  rowwise() %>%
  summarise(min = min(am, gear, carb),
            max = max(am, gear, carb),
            mean = mean(am, gear, carb))

#    min max mean
# 1    1   4    1
# 2    1   4    1
# 3    1   4    1
# 4    0   3    0
# 5    0   3    0
# 6    0   3    0
# 7    0   4    0
# 8    0   4    0
# 9    0   4    0
# 10   0   4    0
# .. ... ...  ...

我猜您想获取每行所有数值变量的平均值、最小值和最大值。

如果您有很多数值变量,首先按照 http://www.jstatsoft.org/v59/i10/

中的描述整理数据可能会更容易

例如,您可以执行以下操作。

library(dplyr)
library(tidyr)

df <- read.csv(text="
translation,category,macrocategory,subcategory,1640,1641,1642,1643,1644
almonds,nuts,Flavoring/Other,,,491,,,
apples,Fruit,Fruits and Vegetables,42,,67,,,
Atlantic herring,Fish,Meat,,52,0,9,,
aurochs,Meat,Meat,game,,4,25,5,
bacon,Meat,Meat,pork,,275.87,78,92,0
barley groats,Grain,Grain,5,9,2,14,56,9
beef,Meat,Meat,Beef,,5.25,,,"
)

info <- 
    df %>%
    # tidy data
    gather(variable, value, -(1:4)) %>%
    # summarise by food item 'translation'
    group_by(translation) %>%
    summarise(
        mean = mean(value, na.rm=TRUE),
        min = min(value, na.rm=TRUE),
        max = max(value, na.rm=TRUE)
    )

这会给你

       translation      mean    min    max
1          almonds 491.00000 491.00 491.00
2           apples  67.00000  67.00  67.00
3 Atlantic herring  20.33333   0.00  52.00
4          aurochs  11.33333   4.00  25.00
5            bacon 111.46750   0.00 275.87
6    barley groats  18.00000   2.00  56.00
7             beef   5.25000   5.25   5.25    

如果你还需要原来的数值变量,你可以加入这个infodf

df %>% left_join(info)

有关更多详细信息,我强烈建议阅读 tidy data paper 并研究 dplyrtidyr 包。