以编程方式添加摘要列

时间:2017-02-08 05:07:28

标签: r dplyr

我有数据框X01,其列应与meanmaxmin

进行汇总
> head(X01)
  B01002e2 B01002e3
1     39.6     47.3
2     37.0     44.8
3     52.6     49.8
4     35.5     26.7
5     39.4     23.9
6     40.8     39.8

我的目标是在每列后添加minmaxmean。到目前为止,我已经通过重新排列列顺序手动完成了此操作,但我很快就会有包含许多列的数据,这使得这种方法非常慢:

X01$B01002e2_min <- min(X01$B01002e2, na.rm = TRUE)
X01$B01002e2_max <- max(X01$B01002e2, na.rm = TRUE)
X01$B01002e2_mean <- mean(X01$B01002e2, na.rm = TRUE)
X01$B01002e3_min <- min(X01$B01002e3, na.rm = TRUE)
X01$B01002e3_max <- max(X01$B01002e3, na.rm = TRUE)
X01$B01002e3_mean <- mean(X01$B01002e3, na.rm = TRUE)

X01 <- X01[ , c(1,3,4,5,2,6,7,8)]

> head(X01)
  B01002e2 B01002e2_min B01002e2_max B01002e2_mean B01002e3 B01002e3_min B01002e3_max
1     39.6            6         83.7    35.3427547     47.3          8.9         90.8
2     37.0            6         83.7    35.3427547     44.8          8.9         90.8
3     52.6            6         83.7    35.3427547     49.8          8.9         90.8
4     35.5            6         83.7    35.3427547     26.7          8.9         90.8
5     39.4            6         83.7    35.3427547     23.9          8.9         90.8
6     40.8            6         83.7    35.3427547     39.8          8.9         90.8
  B01002e3_mean
1    37.6894248
2    37.6894248
3    37.6894248
4    37.6894248
5    37.6894248
6    37.6894248

R中是否有解决方案在一个步骤中处理每个列之后添加这些列,例如addmargins()

dput(head(X01))
    structure(list(B01002e2 = c(39.6, 37, 52.6, 35.5, 39.4, 40.8), 
        B01002e3 = c(47.3, 44.8, 49.8, 26.7, 23.9, 39.8)), .Names = c("B01002e2", 
    "B01002e3"), row.names = c(NA, 6L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

这是尝试使用函数方法遍历每个列和函数:

funs <- c("min","max","mean")
cbind(
  dat,
  unlist(Map(function(f,d) lapply(d,f), mget(funs, inherits=TRUE), list(dat) ), rec=FALSE)
)
#  B01002e2 B01002e3 min.B01002e2 min.B01002e3 max.B01002e2 max.B01002e3 mean.B01002e2 mean.B01002e3
#1     39.6     47.3         35.5         23.9         52.6         49.8      40.81667      38.71667
#2     37.0     44.8         35.5         23.9         52.6         49.8      40.81667      38.71667
#3     52.6     49.8         35.5         23.9         52.6         49.8      40.81667      38.71667
#4     35.5     26.7         35.5         23.9         52.6         49.8      40.81667      38.71667
#5     39.4     23.9         35.5         23.9         52.6         49.8      40.81667      38.71667
#6     40.8     39.8         35.5         23.9         52.6         49.8      40.81667      38.71667

答案 1 :(得分:1)

这是function restrictFile() { var id = '10iM3V2q7FQWBAxy93eN9jvbp_SFco-KLPibeG9XRr71'; // get the file with the Advanced Drive API (REST V2) var file = Drive.Files.get(id); Logger.log('File "%s", restricted label was: %s', file.title, file.labels.restricted); // set the restricted label file.labels.restricted = true; //update the file Drive.Files.update(file, id); // check the updated file var updatedFile = Drive.Files.get(id); Logger.log('File "%s", restricted label is: %s', updatedFile.title, updatedFile.labels.restricted); } 方法:

dplyr
library(dplyr)

X01 %>% mutate_all(funs(max, mean, min))

如果您想忽略 B01002e2 B01002e3 B01002e2_max B01002e3_max B01002e2_mean B01002e3_mean B01002e2_min B01002e3_min 1 39.6 47.3 52.6 49.8 40.81667 38.71667 35.5 23.9 2 37.0 44.8 52.6 49.8 40.81667 38.71667 35.5 23.9 3 52.6 49.8 52.6 49.8 40.81667 38.71667 35.5 23.9 4 35.5 26.7 52.6 49.8 40.81667 38.71667 35.5 23.9 5 39.4 23.9 52.6 49.8 40.81667 38.71667 35.5 23.9 6 40.8 39.8 52.6 49.8 40.81667 38.71667 35.5 23.9 ,则可以添加NA

na.rm=TRUE
X01[3,1] = NA

X01 %>% mutate_all(funs(max, mean, min), na.rm=TRUE)

如果您只想将汇总值作为新数据框,则可以执行以下操作:

  B01002e2 B01002e3 B01002e2_max B01002e3_max B01002e2_mean B01002e3_mean B01002e2_min B01002e3_min
1     39.6     47.3         40.8         49.8         38.46      38.71667         35.5         23.9
2     37.0     44.8         40.8         49.8         38.46      38.71667         35.5         23.9
3       NA     49.8         40.8         49.8         38.46      38.71667         35.5         23.9
4     35.5     26.7         40.8         49.8         38.46      38.71667         35.5         23.9
5     39.4     23.9         40.8         49.8         38.46      38.71667         35.5         23.9
6     40.8     39.8         40.8         49.8         38.46      38.71667         35.5         23.9
X01 %>% summarise_all(funs(max, mean, min), na.rm=TRUE)