我在R中有函数来获取任何东西的平均值,这是dataframe的任何列名
mean_anything <- function(directory, anything){
files_full <- list.files(directory, full.names=TRUE)
seethis <- lapply(files_full, read.csv)
output <- do.call(rbind, seethis)
mean(output$anything, na.rm=TRUE)
}
如果我调用mean_anything("diet_data", "Age")
,我会
[1] NA
Warning message:
In mean.default(output$anything, na.rm = TRUE) :
argument is not numeric or logical: returning NA
但是,如果我更换
mean(output$anything, na.rm=TRUE)
与
mean(output$Age, na.rm=TRUE)
然后该函数将输出[1] 36.4
我尝试在anything
周围使用单引号和双引号,我尝试输出[任何],如何修复?
structure(list(Patient.Name = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("Andy", "David", "John", "Mike", "Steve"), class = "factor"),
Age = c(30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 35L, 35L, 35L, 35L,
35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L,
35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L,
35L, 35L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L,
55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L,
55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L), Weight = c(140L,
140L, 140L, 139L, 138L, 138L, 138L, 138L, 138L, 138L, 138L,
138L, 137L, 137L, 138L, 139L, 139L, 137L, 137L, 137L, 137L,
137L, 137L, 135L, 135L, 135L, 135L, 135L, 135L, 135L, 210L,
209L, 209L, 209L, 209L, 209L, 209L, 208L, 208L, 208L, 208L,
208L, 208L, 207L, 206L, 206L, 206L, 205L, 205L, 205L, 205L,
204L, 204L, 204L, 203L, 203L, 202L, 202L, 202L, 201L, 175L,
175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L,
175L, 175L, 175L, 175L, 175L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 177L, 188L, 188L, 188L, 188L, 189L,
189L, 189L, 189L, 189L, 189L, 189L, 189L, 189L, 189L, 190L,
190L, 190L, 190L, 190L, 190L, 190L, 190L, 190L, 192L, 192L,
192L, 192L, 192L, 192L, 192L, 225L, 225L, 225L, 224L, 224L,
224L, 223L, 223L, 223L, 223L, 223L, 222L, 221L, 221L, 221L,
220L, 220L, 219L, 219L, 219L, 218L, 217L, 217L, 217L, 216L,
215L, 215L, 214L, 214L, 214L), Day = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L)), .Names = c("Patient.Name", "Age", "Weight",
"Day"), row.names = c(NA, 150L), class = "data.frame")
答案 0 :(得分:2)
我解决了。
mean(output[,anything], na.rm=TRUE)
即。
mean_anything <- function(directory, anything){
files_full <- list.files(directory, full.names=TRUE)
seethis <- lapply(files_full, read.csv)
output <- do.call(rbind, seethis)
mean(output[,anything], na.rm=TRUE)
}
anything
是数据框中的任何列