我有一个包含n列的数据框,我需要获取其变量的组合:
E.g:
df <- data.frame(A = c("a1","a2","a3","a4","a5","a6"),
B = c("a1","a1","a3","a3","a5","a5"),
C = c("a1","a1","a1","a3","a4","a4"),
D = c("a1","a1","a1","a3","a4","a5"))
我需要创建一个列表,其中包含n-1个元素,每个元素都包含数据帧变量的所有唯一组合。第一个元素包括从第一个开始到结尾的每个列的唯一值。对于每个后续元素,我需要删除前一个附加数据帧的第一列。像这样:
myList <- list(unique(df[,1:ncol(df)),
unique(df[,2:ncol(df)),
unique(df[,3:ncol(df)))
我设法用for循环来解决这个问题:
myList <- list()
for (i in 1:(ncol(df) - 1)){
myList[[i]] <- unique(df[, i:ncol(df)])
}
但是我想知道是否有更快更优雅的方法来做到这一点。
答案 0 :(得分:1)
使用sapply():
sapply(1:(ncol(df)-1),
FUN = function(x, nc, df) unique(df[, x:nc]), nc = ncol(df), df = df)