Home > Blockchain >  Getting first element of the list in each row in a list-column
Getting first element of the list in each row in a list-column

Time:11-23

How can I get rid of the nested lists and only keep the first element of each list in ColumnB?

ColumnA ColumnB
first c(1, 2, 3)
second c(4, 5, 6)
third c(7, 8, 9)

It should look like this:

ColumnA ColumnB
first 1
second 4
third 7

In , I would try it with a lambda function giving me only the first element of the list.

CodePudding user response:

We can use map to loop over the list column and extract the first element

library(dplyr)
library(purrr)
df1 %>%
    mutate(ColumnB = map_dbl(ColumnB, first))

-output

# A tibble: 3 × 2
  ColumnA ColumnB
  <chr>     <dbl>
1 first         1
2 second        4
3 third         7

Or in base R use sapply to loop over the list and extract the first element

df1$ColumnB <- sapply(df1$ColumnB, `[`, 1)

data

df1 <- structure(list(ColumnA = c("first", "second", "third"), ColumnB = list(
    c(1, 2, 3), c(4, 5, 6), c(7, 8, 9))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -3L))

CodePudding user response:

In case your ColumnB is a real list, then we could also do:

library(tidyr)
library(dplyr)

df1 %>% 
  unnest(ColumnB) %>% 
  group_by(ColumnA) %>% 
  slice(1)
  ColumnA ColumnB
  <chr>     <dbl>
1 first         1
2 second        4
3 third         7

In case your ColumnB is a string, then we could do:

library(dplyr)
library(readr)
df %>% 
  mutate(ColumnB = parse_number(ColumnB))
  ColumnA ColumnB
1   first       1
2  second       4
3   third       7

CodePudding user response:

Here's another method using only dplyr :

library(dplyr)

df1 %>% 
  rowwise() %>% 
  mutate(ColumnB = ColumnB[1]) %>%
  ungroup()
#> # A tibble: 3 x 2
#>   ColumnA ColumnB
#>   <chr>     <dbl>
#> 1 first         1
#> 2 second        4
#> 3 third         7
  • Related