Pasting list in different ways

Hi all,
Below code is returning a tibble as expected that is fine

Method1

df <- tibble(ID = 1:2,
             ColB = list(list(ved = "19", ved_name = "No", vedd = "11") ,
                         list(ved = c("65", "83", "2"), ved_name = c("At", "Re", "Rum"),
                              vedd = c("11", "11", "11"))))

Below is the output

df
# A tibble: 2 x 2
     ID ColB            
  <int> <list>          
1     1 <named list [3]>
2     2 <named list [3]>

But if I try performing above in different way, I am not able to

Method2

There is a list already

List_1
[[1]]
[1] "list(list(ved = "19", ved_name = "No", vedd = "11") ,
                         list(ved = c("65", "83", "2"), ved_name = c("At", "Re", "Rum"),
                              vedd = c("11", "11", "11")))"

When I execute below code, the output is different

df <- tibble(ID = 1: 2, ColB = List_1)
df
# A tibble: 5 x 2
     ID ColB     
  <int> <list>   
1    11 <chr [1]
2    12 <chr [1]>

There is a difference. So the exepected output is getting Output of Method1 using Method2 steps.Is this possible to achieve

I'm unable to reproduce your issue. Both approaches give me the same result.

library(dplyr, warn.conflicts = FALSE)

df <- tibble(ID = 1:2, ColB = list(
  list(ved = "19", ved_name = "No", vedd = "11"),
  list(
    ved = c("65", "83", "2"),
    ved_name = c("At", "Re", "Rum"),
    vedd = c("11", "11", "11")
  )
))

df
#> # A tibble: 2 x 2
#>      ID ColB            
#>   <int> <list>          
#> 1     1 <named list [3]>
#> 2     2 <named list [3]>

List_1 <- list(
  list(ved = "19", ved_name = "No", vedd = "11"),
  list(
    ved = c("65", "83", "2"),
    ved_name = c("At", "Re", "Rum"),
    vedd = c("11", "11", "11")
  )
)

tibble(ID = 1:2, ColB = List_1)
#> # A tibble: 2 x 2
#>      ID ColB            
#>   <int> <list>          
#> 1     1 <named list [3]>
#> 2     2 <named list [3]>

Created on 2020-09-06 by the reprex package (v0.3.0)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.