下面是示例数据,该数据是包含不同数据帧的列表。我想根据以下两个条件从中获取一个数据帧。
第一:
rbind()
ing列,这些列的名称与前一列完全相同。遇到不同的列名称时,请将其和所有列放到最后一个。Banana
,则将列2命名为Banana
,但是将列3命名为Orange
,然后将列4再次命名为Banana
。然后,第1列和第2列将rbind()
,第3列和第4列将被删除。Banana
,则将列2命名为Orange
,但是将列3命名为Banana
,则只有列1才能作为列2的开始名称是不同的,即使与第1列相同,我也不在乎第3列的名称。第二:
lst2
是第一个条件的输出。do.call(rowr::cbind.fill, c(lst2, list(fill = 0)))
以上代码功劳@akrun。任何建议都会有所帮助。
样本数据
list(A = structure(list(`A-DIODE` = c(1.2, 0.4), `A-DIODE` = c(1.3,
0.6)), row.names = c(NA, -2L), class = "data.frame"), B = structure(list(
`B-DIODE` = c(1.4, 0.8), `B-ACC1` = c(1.5, 1), `B-ACC2` = c(1.6,
1.2), `B-ANA0` = c(1.7, 1.4), `B-ANA1` = c(1.8, 1.6), `B-BRICKID` = c(1.9,
1.8), `B-CC0` = c(2L, 2L), `B-CC1` = c(2.1, 2.2), `B-DIGDN` = c(2.2,
2.4), `B-DIGDP` = c(2.3, 2.6), `B-DN1` = c(2.4, 2.8), `B-DN2` = c(2.5,
3), `B-DP1` = c(2.6, 3.2), `B-DP2` = c(2.7, 3.4), `B-SCL` = c(2.8,
3.6), `B-SDA` = c(2.9, 3.8), `B-USB0DN` = 3:4, `B-USB0DP` = c(3.1,
4.2), `B-USB1DN` = c(3.2, 4.4), `B-USB1DP` = c(3.3, 4.6),
`B-ACC1` = c(3.4, 4.8), `B-ACC2` = c(3.5, 5), `B-ANA0` = c(3.6,
5.2), `B-ANA1` = c(3.7, 5.4), `B-BRICKID` = c(3.8, 5.6),
`B-CC0` = c(3.9, 5.8), `B-CC1` = c(4L, 6L), `B-DIGDN` = c(4.1,
6.2), `B-DIGDP` = c(4.2, 6.4), `B-DN1` = c(4.3, 6.6), `B-DN2` = c(4.4,
6.8), `B-DP1` = c(4.5, 7), `B-DP2` = c(4.6, 7.2), `B-SCL` = c(4.7,
7.4), `B-SDA` = c(4.8, 7.6), `B-USB0DN` = c(4.9, 7.8), `B-USB0DP` = c(5L,
8L), `B-USB1DN` = c(5.1, 8.2), `B-USB1DP` = c(5.2, 8.4),
`B-NA` = c(5.3, 8.6), `B-ACC2PWRLKG_0v4` = c(5.4, 8.8), `B-ACC2PWRLKG_0v4` = c(5.5,
9), `B-P_IN_Leak` = c(5.6, 9.2)), row.names = c(NA, -2L), class = "data.frame"))
更新1
在@ØysteinS回答之后,我意识到也应该有第三个条件:
第三:
答案 0 :(得分:1)
这应该可以完成工作:
data <- list(A = structure(list(`A-DIODE` = c(1.2, 0.4), `A-DIODE` = c(1.3,
0.6)), row.names = c(NA, -2L), class = "data.frame"), B = structure(list(
`B-DIODE` = c(1.4, 0.8), `B-ACC1` = c(1.5, 1), `B-ACC2` = c(1.6,
1.2), `B-ANA0` = c(1.7, 1.4), `B-ANA1` = c(1.8, 1.6), `B-BRICKID` = c(1.9,
1.8), `B-CC0` = c(2L, 2L), `B-CC1` = c(2.1, 2.2), `B-DIGDN` = c(2.2,
2.4), `B-DIGDP` = c(2.3, 2.6), `B-DN1` = c(2.4, 2.8), `B-DN2` = c(2.5,
3), `B-DP1` = c(2.6, 3.2), `B-DP2` = c(2.7, 3.4), `B-SCL` = c(2.8,
3.6), `B-SDA` = c(2.9, 3.8), `B-USB0DN` = 3:4, `B-USB0DP` = c(3.1,
4.2), `B-USB1DN` = c(3.2, 4.4), `B-USB1DP` = c(3.3, 4.6),
`B-ACC1` = c(3.4, 4.8), `B-ACC2` = c(3.5, 5), `B-ANA0` = c(3.6,
5.2), `B-ANA1` = c(3.7, 5.4), `B-BRICKID` = c(3.8, 5.6),
`B-CC0` = c(3.9, 5.8), `B-CC1` = c(4L, 6L), `B-DIGDN` = c(4.1,
6.2), `B-DIGDP` = c(4.2, 6.4), `B-DN1` = c(4.3, 6.6), `B-DN2` = c(4.4,
6.8), `B-DP1` = c(4.5, 7), `B-DP2` = c(4.6, 7.2), `B-SCL` = c(4.7,
7.4), `B-SDA` = c(4.8, 7.6), `B-USB0DN` = c(4.9, 7.8), `B-USB0DP` = c(5L,
8L), `B-USB1DN` = c(5.1, 8.2), `B-USB1DP` = c(5.2, 8.4),
`B-NA` = c(5.3, 8.6), `B-ACC2PWRLKG_0v4` = c(5.4, 8.8), `B-ACC2PWRLKG_0v4` = c(5.5,
9), `B-P_IN_Leak` = c(5.6, 9.2)), row.names = c(NA, -2L), class = "data.frame"))
# Use lapply to apply the same function to each data frame in the list.
combined_frames <- lapply(data, function(df){
first_name <- names(df)[[1]]
result <- df[, 1, drop = FALSE]
# Keep adding if name is the same as the first
if (ncol(df) != 1) {
for(i in seq(2, length(names(df)), by = 1)){
if(names(df)[[i]] == names(df)[[1]]){
result <- rbind(result, df[, i, drop = FALSE])
} else {
# Otherwise, break out of loop
break
}
}
}
return(result)
})
# Yes, your suggested code seems to work as expected for the last task
do.call(rowr::cbind.fill, c(combined_frames, list(fill = 0)))
#> A.DIODE B.DIODE
#> 1 1.2 1.4
#> 2 0.4 0.8
#> 3 1.3 0.0
#> 4 0.6 0.0
答案 1 :(得分:1)
一个简单的选择是遍历list
,获取列名的运行长度ID,仅提取等于1的列名,unlist
,转换为data.frame
首先是列名,然后是cbind.fill
,将data.frame的list
绑定在一起
library(data.table)
lst1 <- lapply(data, function(x)
setNames(data.frame(unlist(x[rleid(names(x)) == 1])), names(x)[1]))
do.call(rowr::cbind.fill, c(lst1, list(fill = 0)))
# A.DIODE B.DIODE
#1 1.2 1.4
#2 0.4 0.8
#3 1.3 0.0
#4 0.6 0.0