在R中提取列表列中的前x个数的元素?

  • 本文关键字:元素 提取 列表 r list purrr
  • 更新时间 :
  • 英文 :


我有一个大学的数据框架列,其中大学的每个组件是列表中的元素(部门,大学,城市等)。但它们并不完全相同,我只想提取每个元素的前三个元素。我想这样写:

library(tidyverse)
universities %>%
mutate(Affiliations = map(Affiliations, pluck, 1:3))

但是pluck只选择一个元素。有什么想法吗?

以下是dput的结果:

structure(list(Affiliations = list(c("center for advancing electronics dresden (cfaed) tu dresden", 
" dresden", " 01062", " germany"), c("roxelyn and richard pepper department of communication sciences and disorders", 
" northwestern university", " evanston", " il  60208", " united states"
), c("the hugh knowles hearing research center", " northwestern university", 
" evanston", " il  60208", " united states"), c("lodz university", 
" lodz", " poland"), c("cad department", " l'viv polytechnic national university", 
" l'viv", " ukraine"))), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

您可以尝试自定义lambda函数:

<标题>示例数据
universities
# # A tibble: 5 x 1
#   Affiliations
#   <list>      
# 1 <chr [4]>   
# 2 <chr [5]>   
# 3 <chr [5]>   
# 4 <chr [3]>   
# 5 <chr [4]>

自定义Lambda函数

universities %>% 
mutate(Affiliations = map(Affiliations, ~ .[1:3]))
# # A tibble: 5 x 1
#   Affiliations
#   <list>      
# 1 <chr [3]>   
# 2 <chr [3]>   
# 3 <chr [3]>   
# 4 <chr [3]>   
# 5 <chr [3]>

嵌套宽度(如果需要)

universities %>% 
mutate(Affiliations = map(Affiliations, ~ .[1:3])) %>% 
unnest_wider(Affiliations, names_repair = ~ c("v1", "v2", "v3"))
# # A tibble: 5 x 3
#   v1                                           v2                       v3      
#   <chr>                                        <chr>                    <chr>   
# 1 center for advancing electronics dresden (c~ " dresden"               " 01062"
# 2 roxelyn and richard pepper department of co~ " northwestern universi~ " evans~
# 3 the hugh knowles hearing research center     " northwestern universi~ " evans~
# 4 lodz university                              " lodz"                  " polan~
# 5 cad department                               " l'viv polytechnic nat~ " l'viv"

简单的lapply与括号功能。

res <- lapply(universities$Affiliations, `[`, 1:3)
res
# [[1]]
# [1] "center for advancing electronics dresden (cfaed) tu dresden" " dresden"                                                   
# [3] " 01062"                                                     
# 
# [[2]]
# [1] "roxelyn and richard pepper department of communication sciences and disorders"
# [2] " northwestern university"                                                     
# [3] " evanston"                                                                    
# 
# [[3]]
# [1] "the hugh knowles hearing research center" " northwestern university"                 " evanston"                               
# 
# [[4]]
# [1] "lodz university" " lodz"           " poland"        
# 
# [[5]]
# [1] "cad department"                         " l'viv polytechnic national university" " l'viv" 

如果愿意,可以使用rbind.data.framed。

res.df <- setNames(do.call(rbind.data.frame, res), c("V1", "V2", "V3"))
res.df
#                                                                              V1                                     V2        V3
# 1                   center for advancing electronics dresden (cfaed) tu dresden                                dresden     01062
# 2 roxelyn and richard pepper department of communication sciences and disorders                northwestern university  evanston
# 3                                      the hugh knowles hearing research center                northwestern university  evanston
# 4                                                               lodz university                                   lodz    poland
# 5                                                                cad department  l'viv polytechnic national university     l'viv

最新更新