Biostar Beta. Not for public use.
Subset data frames stored inside a list in R by same column name
1
Entering edit mode
21 months ago
paolo002 • 140

Hi all

I have multiple csv files stored in a list (list.data) having same column names but different data frame names. I would like to subset a column (CHR) by a specific value (1) and apply it to all the data frames in the list and to all the columns.

I wrote this code :

CHR1<-lapply(list.data, function(x) { x["CHR"] = 1; x }

However this seem to subset correctly that specific column (CHR) but the other columns did not change, subset was not applied to the rest of the columns.

I wrote also this code but it returns an empty list

CHR1<-lapply(list.data, subset, "CHR"=1)

Any help highly appreciated. Thanks

R subset • 274 views
ADD COMMENTlink
1
Entering edit mode
4 months ago
zx8754 7.5k
London

Try:

CHR1 <- lapply(list.data, function(x) { x[ x$CHR == 1, ] })
  • = is an assignment operator, e.g.: myVar = 1; print(myVar) same as <-.

  • == is a logical operator, used for comparing, e.g.: 1 == 2 this will give us FALSE.

ADD COMMENTlink
0
Entering edit mode

Thank you very much for your help once again. It worked. And thanks for the explanation.

ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.1