Asssinging to list object and manipulating to dataframe

Hello,

I don't know why I am exactly struggling with this problem but essentially I have multiple functions putting out a set of numbers (as below) and I just want to be able to merge them into one list and finally one dataframe. The final output should be 1,4,NA in the first row with 2,2,1 in the second row etc.

library(tidyverse)

f1 <- c(1,2,3,NA,5)
f2 <- c(4,2,1,3,NA)
f3 <- c(NA,1,2,3,4)

temp_list <- list()
for (x in 1:length(f1)) {
temp_list[x] <- c(f1[x],f2[x],f3[x])
}

I tried a variety of ways such as temp_list[x] <- c(f1[x],f2[x],f3[x]) and others such as temp_list[x] <- f1[x] %>% append(f2[x]) %>% append(f3[x]) but it would seem the problem is not at the right side of the allocation but an issue with temp_list[x].

I'm probably missing something super obvious. Any help would be appreciated :slight_smile:

Unless I'm much mistaken, you're simply after this:

f1 <- c(1, 2, 3, NA, 5)
f2 <- c(4, 2, 1, 3, NA)
f3 <- c(NA, 1, 2, 3, 4)

data.frame(f1, f2, f3)
#>   f1 f2 f3
#> 1  1  4 NA
#> 2  2  2  1
#> 3  3  1  2
#> 4 NA  3  3
#> 5  5 NA  4

Created on 2019-06-04 by the reprex package (v0.3.0)

I didn't really understand why you need to use list, but what I guess from your code, you need to use temp_list[[x]] inside the for loop, instead of temp_list[x].

Hope this helps.

Hello,

Thanks for response. My toy version doesn't work 1:1 to the problem I'm experiencing. So I need this format f1,f2,f3 as in my example I need to read a whole column of data into the function as [,x] and then that output gets saved to f1 and I need \the combination of f1 to fn then to combine to a list where I can then combine the whole thing as a dataframe if that makes sense?

I finally found a solution to my problem. Apologies for not creating the best small reproducible example.

So in terms of the solution, the following worked for my needs:

  temp_list[x] <- cbind(f1[x],f2[x],f3[x]) %>% list()

You can also do some list manipulation to df or modified list using the purrr :package:

some examples:

f1 <- c(1,2,3,NA,5)
f2 <- c(4,2,1,3,NA)
f3 <- c(NA,1,2,3,4)

library(purrr)
# to a df directly
pmap_df(list(f1 = f1, f2 = f2, f3 = f3), tibble::tibble)
#> # A tibble: 5 x 3
#>      f1    f2    f3
#>   <dbl> <dbl> <dbl>
#> 1     1     4    NA
#> 2     2     2     1
#> 3     3     1     2
#> 4    NA     3     3
#> 5     5    NA     4

# to a list of list of three values 
transpose(list(f1 = f1, f2 = f2, f3 = f3)) %>% str()
#> List of 5
#>  $ :List of 3
#>   ..$ f1: num 1
#>   ..$ f2: num 4
#>   ..$ f3: num NA
#>  $ :List of 3
#>   ..$ f1: num 2
#>   ..$ f2: num 2
#>   ..$ f3: num 1
#>  $ :List of 3
#>   ..$ f1: num 3
#>   ..$ f2: num 1
#>   ..$ f3: num 2
#>  $ :List of 3
#>   ..$ f1: num NA
#>   ..$ f2: num 3
#>   ..$ f3: num 3
#>  $ :List of 3
#>   ..$ f1: num 5
#>   ..$ f2: num NA
#>   ..$ f3: num 4

Created on 2019-06-10 by the reprex package (v0.3.0.9000)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.