0

This question is similar to R: How to extract a list from a dataframe? But I could not implement it to my question in an easy way.

weird_df <- data_frame(col1 =c('hello', 'world', 'again'),col_weird = list(list(12,23), list(23,24), NA),col_weird2 = list(list(0,45), list(4,45),list(45,45.45,23)))
weird_df
# A tibble: 3 x 3
  col1  col_weird  col_weird2
  <chr> <list>     <list>    
1 hello <list [2]> <list [2]>
2 world <list [2]> <list [2]>
3 again <lgl [1]>  <list [3]>
> 

I want in the columns col_weirdand col_weird2 to only display the first value of the current list.

col1  col_weird  col_weird2   
1 hello 12       0
2 world 23       4
3 again NA      45

My real problem has a lot of columns.I tried this (altered acceptend answer in posted link)

library(tidyr)
library(purrr)

weird_df %>%
     mutate(col_weird = map(c(col_weird,col_weird2), toString ) ) %>%
     separate(col_weird, into = c("col1"), convert = TRUE) %>%
     separate(col_weird2, into = c("col2",convert = T)

JORIS
  • 59
  • 7
  • I don't know the function `data_frame()`, did you mean `data.frame()`? Also, `data.frame()` does behave differently. What package is `data_frame()` from? – flo Nov 03 '20 at 14:49
  • Actually I have it from the mentioned link. It is right with the underscore ```data_frame```because it should result in a tibble. With ```data.frame``` it results in a data.frame. – JORIS Nov 03 '20 at 15:05

2 Answers2

1

One solution would be to write a simple function that extracts the first value from each list in a vector of lists . This you can then apply to the relevant columns in your data frame.

library(tibble)

#create data
weird_df <- tibble(col1 =c('hello', 'world', 'again'),
                   col_weird = list(list(12,23), list(23,24), NA),
                   col_weird2 = list(list(0,45), list(4,45), list(45,45.45,23)))

#function to extract first values from a vector of lists
fnc <- function(x) {
  sapply(x, FUN = function(y) {y[[1]]})
}

#apply function to the relevant columns
weird_df[,2:3] <- apply(weird_df[,2:3], MARGIN = 2, FUN = fnc)
weird_df

# A tibble: 3 x 3
  col1  col_weird col_weird2
  <chr>     <dbl>      <dbl>
1 hello        12          0
2 world        23          4
3 again        NA         45
AndreasM
  • 902
  • 5
  • 10
1

Here is a dplyr solution

library(dplyr)
weird_df %>% mutate(across(c(col_weird, col_weird2), ~vapply(., `[[`, numeric(1L), 1L)))

Output

# A tibble: 3 x 3
  col1  col_weird col_weird2
  <chr>     <dbl>      <dbl>
1 hello        12          0
2 world        23          4
3 again        NA         45
ekoam
  • 8,744
  • 1
  • 9
  • 22