我正在尝试处理从csv文件读取的数据,其中列包含由数字数据条目列表组成的字符串。例如:
"ID","Nums"
"bob","1, 2, 3, 4, 5, 6, 7, 8"
"ana","8, 7, 6, 5, 4, 3, 2, 1"
例如,如果我尝试计算给定行的nums的平均值,我该如何处理呢?
答案 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)