Ben G
Ben G

Reputation: 4348

Extract the first x number of elements in a list column in R?

I have a dataframe column of universities where each component of the university is an element in a list (department, university, city, etc). But they're not all the same and I'd only like to extract the first three elements of each. I want to something like:

library(tidyverse)

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

But pluck only selects a single element. Any thoughts here?

Here is the results of the 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"))

Upvotes: 0

Views: 60

Answers (2)

the-mad-statter
the-mad-statter

Reputation: 8961

You could try a custom lambda function:

Example Data

universities

# # A tibble: 5 x 1
#   Affiliations
#   <list>      
# 1 <chr [4]>   
# 2 <chr [5]>   
# 3 <chr [5]>   
# 4 <chr [3]>   
# 5 <chr [4]>

Custom Lambda Function

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

Unnest Wider (if desired)

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"

Upvotes: 1

jay.sf
jay.sf

Reputation: 73842

Simply lapply with the bracket function.

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" 

Can be rbind.data.framed if wished.

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

Upvotes: 1

Related Questions