【问题标题】:Subset a dataframe based on a list of values in a list根据列表中的值列表对数据框进行子集
【发布时间】:2020-08-19 09:18:27
【问题描述】:

我有一个包含参与者 ID 和观察结果的数据框。我还有一些需要从该数据框中删除的参与者的 ID 列表 - 我想删除与该参与者 ID 关联的整行。我尝试了以下方法:

ListtoRemove <- as.list(ListtoRemove)
NewDataFrame <-    
subset(OldDataFrame,OldDataFrame$ParticipantsIDs!=ListtoRemove)

这会给出两个警告并且不会删除行。

1: In `!=.default`(DemographicsALL$subject_label, AllSibs) :
longer object length is not a multiple of shorter object length
2: In is.na(e1) | is.na(e2) :
longer object length is not a multiple of shorter object length
> 

数据示例:

structure(list(ParticipantsIDs = structure(c(2L, 1L, 3L, 4L, 
6L, 5L), .Label = c("B0002", "B001", "B003", "B004", "L004", 
"M003"), class = "factor"), Age = structure(c(3L, 1L, 4L, 2L, 
5L, 6L), .Label = c("15", "23", "45", "53", "65", "98"), class =      
"factor")), class = "data.frame", row.names = c(NA, 
-6L))

ListtoRemove <- as.list(B004,M003)

【问题讨论】:

  • 是来自NewDataFramedput
  • B004M003 是什么?变量(如所写)或字符串(当前存在语法错误)?

标签: r dataframe conditional-statements rows


【解决方案1】:
NewDataFrame[ !NewDataFrame[,1] %in% unlist(ListtoRemove), ]
#      ParticipantsIDs Age 
# [1,] "B001"          "45"
# [2,] "B0002"         "15"
# [3,] "B003"          "53"
# [4,] "L004"          "98"

我认为您提供的代码可能存在一些错误。

  1. 您使用subset 的方式表明NewDataFramedata.frame,但您给了我们matrix。无论哪种方式,我的代码都能正常工作,但您的 subset 将会失败(与您展示的方式不同)。
  2. as.list(B004, M003) 最多可能有三点错误:

    • 如果这些是变量的名称,那么我们没有它们;
    • 如果这些是字符串,那么我们看到

      as.list(B004, M003)
      # Error in as.list(B004, M003) : object 'B004' not found
      
    • as.list(1, 2, 3)list - 化第一个参数,此处忽略 2 和 3(所以我们只会看到 "B004",而不是 M003;也许您的意思是 list("B004", "M003")c("B004", "M003")

相反,我使用了

ListtoRemove <- list("B004","M003")

【讨论】:

  • 谢谢。是的,抱歉,我在制作示例时忘记将其设为数据框。我无法使用原始数据框。
  • "no way ... data frame" ...下次请考虑dput(head(x)),而不是给我们一个不正确的结构。当您搜索带有r 标签(url)的问题时,请阅读顶部段落,它建议“使用dput() 获取数据”
  • 谢谢你,无论如何这个帖子确实给了我需要的答案。
【解决方案2】:

如果您使用的是数据框,那么更易于阅读的方法是:

# create data.frame
df <- data.frame(ParticipantsIDs = c("B001", "B0002", "B003", "B004", "M003", "L004"), 
                        Age = c("45", "15", "53", "23", "65", "98"))

# vector containing ids to remove
ids.remove <- c('B004','M003')

df

# subset df by rows where ParticipantsIDs are not found in ids.remove
subset(df, !(ParticipantsIDs %in% ids.remove))

【讨论】:

    【解决方案3】:

    使用您的数据(ListtoRemove 稍作编辑 - 我希望这是正确的):

    data=structure(c("B001", "B0002", "B003", "B004", "M003", "L004", 
    "45", "15", "53", "23", "65", "98"), .Dim = c(6L, 2L), .Dimnames = list(
    NULL, c("ParticipantsIDs", "Age")))
    ListtoRemove <- list("B004","M003")
    

    怎么样:

    data_subset=data[!data[,"ParticipantsIDs"] %in% unlist(ListtoRemove),]
    

    输出:

    > data_subset
         ParticipantsIDs Age 
    [1,] "B001"          "45"
    [2,] "B0002"         "15"
    [3,] "B003"          "53"
    [4,] "L004"          "98"
    

    【讨论】:

      【解决方案4】:

      我最终使用了:

      data_subset = data[!data[, "ParticipantsIDs"] %in% unlist(ListtoRemove), ]
      

      而且效果很好。

      【讨论】:

        猜你喜欢
        • 2013-04-22
        • 2016-12-15
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 2022-11-18
        • 2020-09-09
        • 2017-07-21
        • 1970-01-01
        相关资源
        最近更新 更多