【问题标题】:Subsetting row numbers of data frames stored in list having same values of integers stored in list存储在列表中的数据帧的子集行号具有相同的整数值存储在列表中
【发布时间】:2019-06-19 03:43:04
【问题描述】:

我对处理存储在列表中的数据帧还不是很熟悉。

我有一个整数列表,它们基本上表示两个数据帧的行的索引(df_nameA,df_nameB):

str(list1)
List of 2
$ df_nameA  :int [1:3] 3 4 6
$ df_nameB  :int [1:3] 1 2 4

df_nameA
nrow  col1.  col 3
 1.    a.    A1
 2.    b.    A2
 3     c     B1
 4.    d     B2
 5.    e     C1
 6.    f     C2
df_nameB
nrow  col1.  col 3
 1.    g     D1
 2.    h     D2
 3     i     E1
 4.    l     E2
 5.    m     F1
 6.    n     F2
list2<-list(df_nameA, df_nameB)
str(list2)
List of 2  :  6 observation and 3 variables
  $:'dataframe'....

期望的输出:

df_nameA
nrow  col1.  col 3 
 3     c     B1
 4.    d     B2
 6.    f     C2
df_nameB
nrow  col1.  col 3
 1.    g     D1
 2.    h     D2
 4.    l     E2

基本上,我想根据列表 1 中存储的值对 list2 中的数据帧进行子集化。

我写了这样的东西,但它似乎不起作用:

for(i in seq_along(list1)){
  for(i in seq_along(list2)){
    lapply(list2, function(x) {return(x[x$nrow %in% list1[[i]],])})
  }}

代码似乎从两个数据帧中对第 1、2、4 行进行了子集化 对我的代码有什么建议吗?

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以通过以下方式使用lapply,也可以使用以下for循环

    应用

    names(list2) <- c("df_nameA", "df_nameB")
    lapply(names(list2), function(x) list2[[x]][list2[[x]]$nrow %in% list1[[x]], ])
    
    # [[1]]
    #   nrow col1. col.3
    # 3    3     c    B1
    # 4    4     d    B2
    # 6    6     f    C2
    # 
    # [[2]]
    #   nrow col1. col.3
    # 1    1     g    D1
    # 2    2     h    D2
    # 4    4     l    E2
    

    for循环

    names(list2) <- c("df_nameA", "df_nameB")
    for (x in names(list2)) {
      list2[[x]] <- list2[[x]][list2[[x]]$nrow %in% list1[[x]], ]
    }
    
    list2
    
    # $`df_nameA`
    #   nrow col1. col.3
    # 3    3     c    B1
    # 4    4     d    B2
    # 6    6     f    C2
    # 
    # $df_nameB
    #   nrow col1. col.3
    # 1    1     g    D1
    # 2    2     h    D2
    # 4    4     l    E2
    

    数据
    可重现格式的数据:

    list1 <- list(df_nameA = c(3, 4, 6), 
                  df_nameB = c(1, 2, 4))
    
    list2 <- list(data.frame(nrow = c(1, 2, 3, 4, 5, 6), 
                             col1. = c("a.", "b.", "c", "d", "e", "f"), 
                             col.3 = c("A1", "A2", "B1", "B2", "C1", "C2")), 
                  data.frame(nrow = c(1, 2, 3, 4, 5, 6), 
                             col1. = c("g", "h", "i", "l", "m", "n"), 
                             col.3 = c("D1", "D2", "E1", "E2", "F1", "F2")))
    

    【讨论】:

      【解决方案2】:

      您将在第二个循环中覆盖 i: 这可能有效:

      for(i in seq_along(list1)){
        list2[[i]][ list2[[i]]$nrow %in% list1[[i]],]
      }
      

      【讨论】:

      • 现在您的代码正在获取 df_nameB 并正确设置子集,但我没有得到其他 df 的子集,抱歉,我对此不太熟悉......而且,当我输入 list2 [[i]] 它返回列表的最后一个奇怪的元素
      • 嗨对不起,你的代码是正确的,实际上我需要创建一个空列表并将每个新创建的元素保存在空列表中,否则它会覆盖它
      • for(i in seq_along(list1)){ latest[[i]]
      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2018-09-23
      • 2013-11-09
      • 1970-01-01
      相关资源
      最近更新 更多