extract sublist of dataframes from list of dataframes based on condition
up vote
-1
down vote
favorite
I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work
cond<- sapply(ListofData, function(x) x$treatment == "ZEO")
test <- ListofData[(cond)]
The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list
test<-ListofData[grep('^[Zeo]+',ListofData)]
Can you please help me to find a way to extract the data frames I need?
r regex list dataframe sublist
New contributor
add a comment |
up vote
-1
down vote
favorite
I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work
cond<- sapply(ListofData, function(x) x$treatment == "ZEO")
test <- ListofData[(cond)]
The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list
test<-ListofData[grep('^[Zeo]+',ListofData)]
Can you please help me to find a way to extract the data frames I need?
r regex list dataframe sublist
New contributor
Maybe tryListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
orListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
– zx8754
yesterday
1
@zx8754ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
doesn't work, howeverListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!
– user163731
yesterday
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work
cond<- sapply(ListofData, function(x) x$treatment == "ZEO")
test <- ListofData[(cond)]
The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list
test<-ListofData[grep('^[Zeo]+',ListofData)]
Can you please help me to find a way to extract the data frames I need?
r regex list dataframe sublist
New contributor
I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work
cond<- sapply(ListofData, function(x) x$treatment == "ZEO")
test <- ListofData[(cond)]
The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list
test<-ListofData[grep('^[Zeo]+',ListofData)]
Can you please help me to find a way to extract the data frames I need?
r regex list dataframe sublist
r regex list dataframe sublist
New contributor
New contributor
edited yesterday
zx8754
28.5k76394
28.5k76394
New contributor
asked yesterday
user163731
61
61
New contributor
New contributor
Maybe tryListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
orListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
– zx8754
yesterday
1
@zx8754ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
doesn't work, howeverListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!
– user163731
yesterday
add a comment |
Maybe tryListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
orListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
– zx8754
yesterday
1
@zx8754ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
doesn't work, howeverListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!
– user163731
yesterday
Maybe try
ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
– zx8754
yesterday
Maybe try
ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
– zx8754
yesterday
1
1
@zx8754
ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!– user163731
yesterday
@zx8754
ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!– user163731
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Solution using regex should work, see this example:
#example data
ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
xx1 = data.frame(xx = 2, treatment = "xx"),
ZEO2= data.frame(xx = 3, treatment = "ZEO"))
#using regex
res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
Here is another solution using column values, this returns empty dataframes, which we exclude using nrow
and subset:
# using lapply, then filter
res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
res <- res[ sapply(res, nrow) > 0 ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Solution using regex should work, see this example:
#example data
ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
xx1 = data.frame(xx = 2, treatment = "xx"),
ZEO2= data.frame(xx = 3, treatment = "ZEO"))
#using regex
res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
Here is another solution using column values, this returns empty dataframes, which we exclude using nrow
and subset:
# using lapply, then filter
res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
res <- res[ sapply(res, nrow) > 0 ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
add a comment |
up vote
0
down vote
accepted
Solution using regex should work, see this example:
#example data
ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
xx1 = data.frame(xx = 2, treatment = "xx"),
ZEO2= data.frame(xx = 3, treatment = "ZEO"))
#using regex
res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
Here is another solution using column values, this returns empty dataframes, which we exclude using nrow
and subset:
# using lapply, then filter
res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
res <- res[ sapply(res, nrow) > 0 ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Solution using regex should work, see this example:
#example data
ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
xx1 = data.frame(xx = 2, treatment = "xx"),
ZEO2= data.frame(xx = 3, treatment = "ZEO"))
#using regex
res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
Here is another solution using column values, this returns empty dataframes, which we exclude using nrow
and subset:
# using lapply, then filter
res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
res <- res[ sapply(res, nrow) > 0 ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
Solution using regex should work, see this example:
#example data
ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
xx1 = data.frame(xx = 2, treatment = "xx"),
ZEO2= data.frame(xx = 3, treatment = "ZEO"))
#using regex
res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
Here is another solution using column values, this returns empty dataframes, which we exclude using nrow
and subset:
# using lapply, then filter
res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
res <- res[ sapply(res, nrow) > 0 ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO
answered yesterday
zx8754
28.5k76394
28.5k76394
add a comment |
add a comment |
user163731 is a new contributor. Be nice, and check out our Code of Conduct.
user163731 is a new contributor. Be nice, and check out our Code of Conduct.
user163731 is a new contributor. Be nice, and check out our Code of Conduct.
user163731 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372227%2fextract-sublist-of-dataframes-from-list-of-dataframes-based-on-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe try
ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
orListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
– zx8754
yesterday
1
@zx8754
ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]
doesn't work, howeverListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!– user163731
yesterday