处理多值列中的数值数据

时间:2018-03-27 22:09:02

标签: r

我正在尝试处理从csv文件读取的数据,其中列包含由数字数据条目列表组成的字符串。例如:

"ID","Nums"
"bob","1, 2, 3, 4, 5, 6, 7, 8"
"ana","8, 7, 6, 5, 4, 3, 2, 1"

例如,如果我尝试计算给定行的nums的平均值,我该如何处理呢?

1 个答案:

答案 0 :(得分:1)

一种选择是使用strsplit拆分,,然后将mapply功能应用为:

df$mean <- mapply(function(x)mean(as.numeric(x)), strsplit(df$Nums, ","))
df
#   ID                   Nums mean
#1 bob 1, 2, 3, 4, 5, 6, 7, 8   4.5
#2 ana 8, 7, 6, 5, 4, 3, 2, 1   4.5

数据

df <- read.csv(text =
'"ID","Nums"
"bob","1, 2, 3, 4, 5, 6, 7, 8"
"ana","8, 7, 6, 5, 4, 3, 2, 1"', header = TRUE, stringsAsFactors = FALSE)