Tom
Tom

Reputation: 2351

Using the length of the dataframe to subset a list with lapply

My data is as follows:

library(data.table)   
df <- fread(
    "A   B  C  D  E  F  iso   year   
     0   A   NA  1  NA  NA  NLD   2009   
     1   Y   NA  2  NA  NA  NLD   2009   
     0   Q   NA  3  NA  NA  AUS   2011   
     1   NA  NA  4  NA  NA  AUS   2011   
     0   0   NA  7  NA  NA  NLD   2008   
     1   1   NA  1  NA  NA  NLD   2008   
     0   1   NA  3  NA  NA  AUS   2012   
     0   NA  1   NA  1  NA  ECU   2009   
     1   NA  0   NA  2  0   ECU   2009   
     0   NA  0   NA  3  0   BRA   2011   
     1   NA  0   NA  4  0   BRA   2011   
     0   NA  1   NA  7  NA  ECU   2008   
     1   NA  0   NA  1  0   ECU   2008   
     0   NA  0   NA  3  2   BRA   2012   
     1   NA  0   NA  4  NA  BRA   2012",
   header = TRUE
)

# Creates a list of dataframes
df_iso <- split(df, df$iso) # Creates a list of dataframes

I would from every dataframe in the list like to select certain rows, let us say:

subset <- as.vector(lapply(df_iso, '[', 2:3, ))

However, in reality I do not know how long each dataframe in the list is, so I want to do something like:

subset<- as.vector(lapply(df_iso, '[', 2:nrow(df_iso[[i]][,1]), ))

Which instead of providing a number directly is supposed to check how many row the dataframe has first.

But even though: nrow(df_iso[[1]][,1]) gives 3, the code as a whole gives the error: Error: argument of length 0

Now I could obviously reverse the subsetting and do:

subset <- as.vector(lapply(df_iso, '[', -c(1), ))

But I would still like to now the proper syntax for what I intended to do.

Upvotes: 0

Views: 159

Answers (1)

Jrm_FRL
Jrm_FRL

Reputation: 1423

Why not simply :

lapply(df_iso, function(x) {x[2:nrow(x),]})

# $AUS
# A    B  C D  E  F iso year
# 1: 1 <NA> NA 4 NA NA AUS 2011
# 2: 0    1 NA 3 NA NA AUS 2012
# 
# $BRA
# A    B C  D E  F iso year
# 1: 1 <NA> 0 NA 4  0 BRA 2011
# 2: 0 <NA> 0 NA 3  2 BRA 2012
# 3: 1 <NA> 0 NA 4 NA BRA 2012
# 
# $ECU
# A    B C  D E  F iso year
# 1: 1 <NA> 0 NA 2  0 ECU 2009
# 2: 0 <NA> 1 NA 7 NA ECU 2008
# 3: 1 <NA> 0 NA 1  0 ECU 2008
# 
# $NLD
# A B  C D  E  F iso year
# 1: 1 Y NA 2 NA NA NLD 2009
# 2: 0 0 NA 7 NA NA NLD 2008
# 3: 1 1 NA 1 NA NA NLD 2008

Upvotes: 1

Related Questions