分析列表中的数据框并绑定结果 [英] Analyze Data Frames In A List And Bind The Results

查看:88
本文介绍了分析列表中的数据框并绑定结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有的数据是数据帧的列表.我想遍历每个数据框以找到:

The data I have is a list of data frames. I want to loop through each of the data frame to find:

  1. 如果有重复的列名的列.如果是,那我 想要通过在父数据框中使用rbind()合并它们 称为output并删除此类数据帧的所有其他列.
  2. 我还想检查是否有没有重复的数据框 列.如果是,则删除除第一列以外的所有列.然后 cbind()output一起使用,这样,如果行多于或少于行数 (1)创建的,则应添加zero.
  1. If there are columns with duplicate column names. If yes, then I want to merge them by using rbind() in a parent data frame called output and remove all other columns of such data frames.
  2. I also want to check if there is any data frame that doesn't have duplicate columns. If yes, then remove all the columns except the first one. Then cbind() with output such that if rows are more or less than what was created by (1) then zero should be added.

我尝试使用lappy(),但是我的逻辑不能一次超过两个.任何建议都会有所帮助.

I tried using lappy(), but my logic to get above two isn't working at one go. Any suggestion will help.

output <- lapply(data, function(x) {

})

包含数据框的输入数据列表

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"))

所需的输出

> A
  A-DIODE
    1.2
    0.4
    1.3
    0.6

> B
  B-DIODE
    1.4 
    0.8

> Output
  A-DIODE B-DIODE
    1.2     1.4
    0.4     0.8
    1.3      0
    0.6      0

推荐答案

遍历list,使用if/else创建一个条件,该条件检查unique列名称的length并返回unlist如果只有一个唯一列,则返回单个data.frame,否则返回第一列.最后,使用cbind.fill(来自rowr)将data.frame列的list绑定在一起,将fill指定为0

Loop through the list, create a condition with if/else that checks the length of the unique column names and returns the unlisted single data.frame when there is only a single unique column or else return the first column. Finally, with cbind.fill (from rowr) bind the list of data.frame columns together, specifying the fill as 0

lst2 <- lapply(lst1, function(x) if(length(unique(names(x))) ==1)
     setNames(data.frame(unlist(x)), names(x)[1]) else x[1])
do.call(rowr::cbind.fill, c(lst2, 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

这篇关于分析列表中的数据框并绑定结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆