2

I am looking at extracting df's from within a list of multiple df's into separate data frames based on a condition (if the column names of a df within the list contains the name I am looking for).

For illustration purposes I have created an example which resembles the situation I am in.

I have list with multiple data frames and the dput of that list is given below:

structure(list(V1 = structure(list(lvef = c(0.965686195194885, 
0.0806777632648268, -0.531729196500083, -0.511913109608259, -0.413670941196816, 
-0.0501899795864357, -0.337583918771946, 1.16086745780346, -0.478358865835724, 
-1.95009138673888), hbc = c(-0.389950511350405, -0.904388183933348, 
0.811821977223064, -0.868381700124344, -0.637307418402866, -1.04703715824204, 
-0.394340445217658, -0.194653869597247, 0.00822402232044511, 
-0.145032587618231), id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = "NA", class = "factor")), .Names = c("lvef", 
"hbc", "id"), row.names = c(NA, -10L), class = "data.frame"), 
V2 = structure(list(ersta = c(-0.254360310986174, 0.3859806928747, 
-0.135741797055127, 1.03929145413636, -0.484219739337178, 
0.255476285148917, 1.0479422937128, 0.146613094683722, -0.914377222535014, 
1.75052418161618, -0.275059500684816, 2.34861397588234, 0.00183723766664941, 
0.97612891408903, 0.278868537504227, 0.456979477254684, 1.46323739326792, 
0.664511602217853, 0.870420202897545, 1.38228375734407), 
pgrsta = c(-1.49129812271989, 0.820330747101906, -0.0469488167129374, 
0.471549380446308, -1.71312120132398, 0.0578140025416816, 
1.67016363826724, 0.226180835709491, -2.00294530465909, 
-0.0464857361954717, 0.306942902768782, -0.785096914460742, 
0.283822632249141, -0.260774679911329, -1.2865970194309, 
0.307972619170242, 0.223715024597144, -1.01642533651475, 
-0.12229427204957, 0.223326519096996), id = structure(c(7L, 
7L, 7L, 7L, 4L, 1L, 3L, 5L, 6L, 2L, 7L, 7L, 7L, 7L, 4L, 
1L, 3L, 5L, 6L, 2L), class = "factor", .Label = c("-0.10863576856322", 
"-0.317324527228699", "-0.422764348315332", "0.285132258310185", 
"1.23305496219042", "1.39326602279981", "NA"))), .Names = c("ersta", 
"pgrsta", "id"), row.names = c(NA, -20L), class = "data.frame"), 
V3 = structure(list(hormrec = 1:15, event = structure(c(10L, 
10L, 10L, 10L, 10L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", 
"NA"), class = "factor")), .Names = c("hormrec", "event"), row.names = c(NA, 
-15L), class = "data.frame"), V4 = structure(list(asat = c(-0.321423784000631, 
0.181345361079582, 0.389158724418319, -1.15251833725336, 
-0.351981383678293, -0.506888212379408, 0.870705917350059, 
-0.626883041051641, -0.321843006223371, -0.674564527029912, 
-0.609383943267379, -0.181661119817784, -1.63676077872658
), lab = structure(c(1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 2L), .Label = c("btest", "NA", "rtest"), class = "factor")), .Names = c("asat", 
"lab"), row.names = c(NA, -13L), class = "data.frame")), .Names = c("V1", 
"V2", "V3", "V4"))

I am trying to extract data frames from the list based on the condition that if a data frame within the list contains the column name/s required then that data frame from the list should go into a separate data frame. So far, I have been able to extract the data frames into a list using the following code:

# function to extract required df's
        trial <- function(x) 
    {  
      reqname <- c("hbc","ersta") # column names to check for
      data <- x

      lapply(seq(data), function(i){ # loop through all the data frames in the list
        y <- data.frame(data[[i]]) # extract df in y
        names <- names(y) # extract names of df
        for(a in 1:length(reqname)) # loop through the length of reqname
        {
        if(reqname[a]%in%names) # check if column name/s present in current df
            {
              z <- y # extract df into another df
                return(z) # return df
            }
        }
      }
          )
    }

The above function returns a list of matching df's along with nulls where there was not a match. I am looking for a modification so that the selected data frame comes out separately. If there are two df's matching the requirement then the output should be two separate data frames.

I will appreciate all and any help in finding a solution.

5
  • What is your expected output? If you want to remove the NULL elements, lst1 <- Filter(length, trial(lst)) If you need some data.frame objects in the global environment, list2env(setNames(lst1, paste0('dat' seq_along(lst1))), envir=.GlobalEnv)
    – akrun
    Aug 27, 2015 at 14:12
  • My expected output will be multiple data frames but not in a list. Currently the function returns a list with NULL elements. e.g., using the example list and function in the post - I should have two seperate data frames as a result. I hope this helps.
    – syebill
    Aug 27, 2015 at 14:16
  • Just try the list2env. It will be two dataframe objects.
    – akrun
    Aug 27, 2015 at 14:21
  • This list2env statement should be placed within the IF block? can you ellaborate what is "dat"? should the statement be list2env(setName(z,paste0("dat",seqalong(z))), envir=.GlobalEnv)?
    – syebill
    Aug 27, 2015 at 14:31
  • By using paste0('dat', ...), we name the list elements as 'dat1' and 'dat2'. after the list2env, you can type dat1 and get the first data.frame object
    – akrun
    Aug 27, 2015 at 14:32

2 Answers 2

3

You can easily use the lapply() plus a custom function to identify wanted outputs. For instance, if k is your list,

trial <- function(x) 
    {
        reqnames <- c("hbc","ersta")
        k <- lapply(k, function(x)  any(names(x) %in% reqnames))
        k <- which(k==1)
        x[k]
    }

This outputs a list with only the dataframes containing at least one of the names in reqnames.

0
1

We can remove the NULL elements with Filter

 lst1 <- Filter(length, trial(lst))

If we need multiple data.frame objects in the global environment, use list2env after renaming the list elements with the object names

 names(lst1) <- paste0('dat' seq_along(lst1))
 list2env(lst1, envir = .GlobalEnv)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.