简体   繁体   English

在列表列的每一行中获取列表的第一个元素

[英]Getting first element of the list in each row in a list-column

How can I get rid of the nested lists and only keep the first element of each list in ColumnB ?我怎样才能摆脱嵌套列表,只保留ColumnB中每个列表的第一个元素?

ColumnA列A ColumnB B列
first第一的 c(1, 2, 3) c(1, 2, 3)
second第二 c(4, 5, 6) c(4, 5, 6)
third第三 c(7, 8, 9) c(7, 8, 9)

It should look like this:它应该是这样的:

ColumnA列A ColumnB B列
first第一的 1 1个
second第二 4 4个
third第三 7 7

In , I would try it with a lambda function giving me only the first element of the list.中,我会尝试使用 lambda function 只给我列表的第一个元素。

We can use map to loop over the list column and extract the first element我们可以使用map遍历list列并提取第first元素

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或者在base R中使用sapply遍历list并提取第一个元素

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))

In case your ColumnB is a real list, then we could also do:如果你的 ColumnB 是一个真正的列表,那么我们也可以这样做:

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:如果您的 ColumnB 是一个字符串,那么我们可以这样做:

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

Here's another method using only dplyr :这是另一种仅使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM