我有一个带有定量列的数据集,我想要基于组来计算平均值。数据集中的其他列标题为[FY2001,FY2002,...,FY2018]。这些列填充为1或0。
我想为每一个FY列等于1时计算第一列的平均值。因此,我想要18种不同的平均值。
我习惯于在SAS中使用宏,在那里我可以使用let语句替换数据集名称或列名称的一部分。这是我尝试在R中编写一个循环来解决此问题:
vector = c("01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16","17","18")
varlist = paste("FY20", vector, sep = "")
abc = for (i in length(varlist)){
table(ALL_FY2$paste(varlist)[i])
}
abc
这不起作用,因为它将粘贴功能视为一列。我想念什么?任何帮助将不胜感激。
答案 0 :(得分:1)
我们可以使用Body: Uint8Array(94832) [37, 80, 68, 70, 45, 49, 46, 51, 13, 10, 37, 129, 150, 189, 221, 13, 10, 49, 32, 48, 32, 111, 98, 106, 13, 10, 60, 60, 32, 47, 84, 121, 112, 101, 32, 47, 67, 97, 116, 97, 108, 111, 103, 13, 10, 47, 80, 97, 103, 101, 115, 32, 50, 32, 48, 32, 82, 13, 10, 47, 79, 117, 116, 108, 105, 110, 101, 115, 32, 51, 32, 48, 32, 82, 13, 10, 62, 62, 13, 10, 101, 110, 100, 111, 98, 106, 13, 10, 52, 32, 48, 32, 111, 98, 106, 13, 10, 60, 60, 47, …]
ContentType: "application/pdf"
LastModified: Wed Sep 26 2018 15:34:59 GMT-0400 (Eastern Daylight Time) {}
Metadata: {}
而不是[[
来对列进行子集化。另外,“ abc”应该是一个&
,并为其分配list
循环中每一列的相应table
输出。
for
遍历“ varlist”而不是abc <- vector("list", length(varlist)) # initialize a `list` object
(它是一个数字)的顺序
length(varlist)
但是,如果我们需要从'varlist'中提到的所有列中获得单个for(i in seq_along(varlist)) abc[[i]] <- table(ALL_FY2[[varlist[i]]])
输出,则将table
列分别与unlist
和vector
关联应用rep
table