我希望确定一种将单个数据帧转换为数据帧列表的有效方法。以下是我的可复制MWE:
set.seed(1)
ABAge = runif(100)
ABPoints = rnorm(100)
ACAge = runif(100)
ACPoints = rnorm(100)
BCAge = runif(100)
BCPoints = rnorm(100)
A_B <- data.frame(ID = as.character(paste0("ID", 1:100)), Age = ABAge, Points = ABPoints)
A_C <- data.frame(ID = as.character(paste0("ID", 1:100)), Age = ACAge, Points = ACPoints)
B_C <- data.frame(ID = as.character(paste0("ID", 1:100)), Age = BCAge, Points = BCPoints)
A_B$ID <- as.character(A_B$ID)
A_C$ID <- as.character(A_C$ID)
B_C$ID <- as.character(B_C$ID)
listFormat <- list("A_B" = A_B, "A_C" = A_C, "B_C" = B_C)
dfFormat <- data.frame(ID = as.character(paste0("ID", 1:100)), A_B.Age = ABAge, A_B.Points = ABPoints, A_C.Age = ACAge, A_C.Points = ACPoints, B_C.Age = BCAge, B_C.Points = BCPoints)
dfFormat$ID = as.character(dfFormat$ID)
这将导致数据帧格式(dfFormat
)如下所示:
'data.frame': 100 obs. of 7 variables:
$ ID : chr "ID1" "ID2" "ID3" "ID4" ...
$ A_B.Age : num 0.266 0.372 0.573 0.908 0.202 ...
$ A_B.Points: num 0.398 -0.612 0.341 -1.129 1.433 ...
$ A_C.Age : num 0.6737 0.0949 0.4926 0.4616 0.3752 ...
$ A_C.Points: num 0.409 1.689 1.587 -0.331 -2.285 ...
$ B_C.Age : num 0.814 0.929 0.147 0.75 0.976 ...
$ B_C.Points: num 1.474 0.677 0.38 -0.193 1.578 ...
以及如下所示的数据帧列表listFormat
:
List of 3
$ A_B:'data.frame': 100 obs. of 3 variables:
..$ ID : chr [1:100] "ID1" "ID2" "ID3" "ID4" ...
..$ Age : num [1:100] 0.266 0.372 0.573 0.908 0.202 ...
..$ Points: num [1:100] 0.398 -0.612 0.341 -1.129 1.433 ...
$ A_C:'data.frame': 100 obs. of 3 variables:
..$ ID : chr [1:100] "ID1" "ID2" "ID3" "ID4" ...
..$ Age : num [1:100] 0.6737 0.0949 0.4926 0.4616 0.3752 ...
..$ Points: num [1:100] 0.409 1.689 1.587 -0.331 -2.285 ...
$ B_C:'data.frame': 100 obs. of 3 variables:
..$ ID : chr [1:100] "ID1" "ID2" "ID3" "ID4" ...
..$ Age : num [1:100] 0.814 0.929 0.147 0.75 0.976 ...
..$ Points: num [1:100] 1.474 0.677 0.38 -0.193 1.578 ...
我希望提出一种自动方法将转换dfFormat
为listFormat
。从以上对象可以看出,有两个主要条件:
该列ID
始终是中的第一列,dfFormat
并且始终是的每个子列表中的第一列listFormat
。
子列表的数量等于dfFormat
下划线(_)之前的唯一列名称的数量。在这种情况下,这是三个前缀(例如“ A_B”,“ A_C”和“ B_C”)。这些前缀也是三个子列表的名称。
在每个子列表中,它包含具有关联的前缀(“ A_B”)的列数。对于每个子列表,这是两个(“年龄”和“分数”)。这些后缀是列的名称。
我在这里提出了相反的问题(即如何从listFormat
转到dfFormat
),并从中学习了一些有用的答案。我需要具有使两个方向都反向的代码,并且似乎反向可能需要新类型的代码。我将我的尝试放在下面,以显示我如何被卡住!
conUnd <- which(sapply(colnames(dfFormat), function(x) grepl("_", x)))
listName <- sapply(colnames(dfFormat[,conUnd]), function(x) strsplit(x, "[.]")[[1]][1])
uListName <- unique(sapply(colnames(dfFormat[,conUnd]), function(x) strsplit(x, "[.]")[[1]][1]))
listCol <- sapply(colnames(dfFormat[,conUnd]), function(x) strsplit(x, "[.]")[[1]][2])
listFormat = list()
for (i in 1:length(uListName)){
[Gets messy here trying to define column names based on string variables]
}
任何建议将不胜感激。我知道我的代码效率不高。
您可以split.default
在基数R中使用-
output <- lapply(split.default(dfFormat[-1], sub("\\..*", "",names(dfFormat[-1]))),
function(x) cbind(dfFormat[1], setNames(x, sub(".*\\.", "", names(x)))))
str(output)
#List of 3
# $ A_B:'data.frame': 100 obs. of 3 variables:
# ..$ ID : chr [1:100] "ID1" "ID2" "ID3" "ID4" ...
# ..$ Age : num [1:100] 0.266 0.372 0.573 0.908 0.202 ...
# ..$ Points: num [1:100] 0.398 -0.612 0.341 -1.129 1.433 ...
# $ A_C:'data.frame': 100 obs. of 3 variables:
# ..$ ID : chr [1:100] "ID1" "ID2" "ID3" "ID4" ...
# ..$ Age : num [1:100] 0.6737 0.0949 0.4926 0.4616 0.3752 ...
# ..$ Points: num [1:100] 0.409 1.689 1.587 -0.331 -2.285 ...
# $ B_C:'data.frame': 100 obs. of 3 variables:
# ..$ ID : chr [1:100] "ID1" "ID2" "ID3" "ID4" ...
# ..$ Age : num [1:100] 0.814 0.929 0.147 0.75 0.976 ...
# ..$ Points: num [1:100] 1.474 0.677 0.38 -0.193 1.578 ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句