gpt4 book ai didi

r - 将单个数据框转换为数据框列表(将列名解析为前缀和后缀)

转载 作者:行者123 更新时间:2023-12-04 11:08:32 25 4
gpt4 key购买 nike

我希望确定一种将单个数据帧转换为数据帧列表的有效方法。以下是我可重现的 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 的方法。从上面的对象中可以看出,主要有两个条件:

  1. ID 始终是 dfFormat 中的第一列,并且始终是 listFormat 的每个子列表中的第一列。

  2. 子列表的数量等于 dfFormat 中下划线('_')前的唯一列名的数量。在这种情况下,这是三个前缀(例如“A_B”、“A_C”和“B_C”)。这些前缀也是三个子列表的名称。

  3. 在每个子列表中,它包含具有关联前缀(“A_B”)的列数。对于每个子列表,这是两个(“年龄”和“分数”)。这些后缀是列的名称。

我问了相反的问题here (即如何从 listFormatdfFormat)并得到一些有用的答案,我正在从中学习。我需要有代码来反转两个方向,似乎反转方向可能需要新类型的代码。我把我的尝试放在下面来展示我是如何被困住的!

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]
}

如有任何建议,我们将不胜感激。我知道我的代码效率不高。

最佳答案

您可以在基础 R 中使用 split.default -

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 ...

关于r - 将单个数据框转换为数据框列表(将列名解析为前缀和后缀),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60249889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com