将行中的值合并为r中的一列

时间:2018-10-13 23:29:39

标签: r dplyr tidyr

我想将每一行中每部电影的不同流派组合到一个名为genre的列中。

理想情况下,我想将其作为最终输出:

enter image description here

这是我的样本数据集:

structure(list(g1 = c("Action", "Action"), g2 = c("Adventure", 
"Adventure"), g3 = c("Fantasy", "Fantasy"), g4 = c("Sci-Fi", 
NA), g5 = c(NA_character_, NA_character_), g6 = c(NA_character_, 
NA_character_), g7 = c(NA_character_, NA_character_), g8 = c(NA_character_, 
NA_character_)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

我尝试过

test %>% gather(g1:g8, key = "genre", value = "value")

2 个答案:

答案 0 :(得分:1)

library(dplyr)
library(tidyr)

df <- structure(list(g1 = c("Action", "Action"), g2 = c("Adventure", 
"Adventure"), g3 = c("Fantasy", "Fantasy"), g4 = c("Sci-Fi", 
NA), g5 = c(NA_character_, NA_character_), g6 = c(NA_character_, 
NA_character_), g7 = c(NA_character_, NA_character_), g8 = c(NA_character_, 
NA_character_)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

df %>% 
  mutate(id = row_number()) %>%
  gather("key", "genre", -id) %>% 
  arrange(id, genre)

#> # A tibble: 16 x 3
#>       id key   genre      
#>    <int> <chr> <chr>    
#>  1     1 g1    Action   
#>  2     1 g2    Adventure
#>  3     1 g3    Fantasy  
#>  4     1 g4    Sci-Fi   
#>  5     1 g5    <NA>     
#>  6     1 g6    <NA>     
#>  7     1 g7    <NA>     
#>  8     1 g8    <NA>     
#>  9     2 g1    Action   
#> 10     2 g2    Adventure
#> 11     2 g3    Fantasy  
#> 12     2 g4    <NA>     
#> 13     2 g5    <NA>     
#> 14     2 g6    <NA>     
#> 15     2 g7    <NA>     
#> 16     2 g8    <NA>

reprex package(v0.2.0)于2018-10-13创建。

答案 1 :(得分:1)

这是另一种解决方案,它使用map将函数(转置数据并将行名作为列添加)应用于每一行:

library(tidyverse)

df %>%
  group_by(id = row_number()) %>%
  nest() %>%
  mutate(d = map(data, ~{data.frame(genre = t(.x), stringsAsFactors = F) %>% 
                         rownames_to_column("g")})) %>%
  unnest(d)

# # A tibble: 16 x 3
#      id g     genre    
#   <int> <chr> <chr>    
# 1     1 g1    Action   
# 2     1 g2    Adventure
# 3     1 g3    Fantasy  
# 4     1 g4    Sci-Fi   
# 5     1 g5    NA       
# 6     1 g6    NA       
# 7     1 g7    NA       
# 8     1 g8    NA       
# 9     2 g1    Action   
#10     2 g2    Adventure
#11     2 g3    Fantasy  
#12     2 g4    NA       
#13     2 g5    NA       
#14     2 g6    NA       
#15     2 g7    NA       
#16     2 g8    NA