根据列表中的值列表对数据框进行子集化
Subset a dataframe based on a list of values in a list
我有一个包含参与者 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)
NewDataFrame[ !NewDataFrame[,1] %in% unlist(ListtoRemove), ]
# ParticipantsIDs Age
# [1,] "B001" "45"
# [2,] "B0002" "15"
# [3,] "B003" "53"
# [4,] "L004" "98"
我认为您提供的代码中可能存在一些错误。
- 您使用
subset
的方式表明 NewDataFrame
是 data.frame
,但您给了我们一个 matrix
。我的代码以任何一种方式工作,但你的 subset
会失败(与你展示的方式不同)。
as.list(B004, M003)
最多可能错了三点:
- 如果这些是变量名,那么我们没有它们;
如果这些是字符串,那么我们会看到
as.list(B004, M003)
# Error in as.list(B004, M003) : object 'B004' not found
as.list(1, 2, 3)
仅 list
-ifies 第一个参数,这里 2 和 3 被忽略(所以我们只会看到 "B004"
,而不是 M003
;也许你意思是 list("B004", "M003")
或 c("B004", "M003")
?
相反,我使用了
ListtoRemove <- list("B004","M003")
如果您使用的是数据框,一种更易于阅读的方法是:
# 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))
使用您的数据(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"
我最终使用了:
data_subset = data[!data[, "ParticipantsIDs"] %in% unlist(ListtoRemove), ]
而且效果很好。
我有一个包含参与者 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)
NewDataFrame[ !NewDataFrame[,1] %in% unlist(ListtoRemove), ]
# ParticipantsIDs Age
# [1,] "B001" "45"
# [2,] "B0002" "15"
# [3,] "B003" "53"
# [4,] "L004" "98"
我认为您提供的代码中可能存在一些错误。
- 您使用
subset
的方式表明NewDataFrame
是data.frame
,但您给了我们一个matrix
。我的代码以任何一种方式工作,但你的subset
会失败(与你展示的方式不同)。 as.list(B004, M003)
最多可能错了三点:- 如果这些是变量名,那么我们没有它们;
如果这些是字符串,那么我们会看到
as.list(B004, M003) # Error in as.list(B004, M003) : object 'B004' not found
as.list(1, 2, 3)
仅list
-ifies 第一个参数,这里 2 和 3 被忽略(所以我们只会看到"B004"
,而不是M003
;也许你意思是list("B004", "M003")
或c("B004", "M003")
?
相反,我使用了
ListtoRemove <- list("B004","M003")
如果您使用的是数据框,一种更易于阅读的方法是:
# 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))
使用您的数据(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"
我最终使用了:
data_subset = data[!data[, "ParticipantsIDs"] %in% unlist(ListtoRemove), ]
而且效果很好。