简体   繁体   English

如果指定值与使用 R 的行相对应,如何将宽数据集中的列名作为长数据集中的行

[英]How to bring column name from wide dataset as row in long dataset if specified value corresponded with row using R

the input dataset shows a "wide" dataset that includes unique actors and next to their name are corresponding movies as column name with a 1 assigned if movie corresponds to actors portfolio.输入数据集显示了一个“宽”数据集,其中包括唯一的演员,并且在他们的名字旁边是对应的电影作为列名,如果电影对应于演员组合,则分配 1。

structure(list(Actor = c("Brad Pitt", "Matt Damon", "Leonardo Dicaprio", 
"Kate Winslet", "Jennifer Connoley", "Jude Law", "Gwenyth Paltrow"
), `Once upon a time in america` = c(NA, NA, NA, NA, 1, NA, NA
), `The Departed` = c(NA, 1, 1, NA, NA, NA, NA), `Once Upon a time in Hollywood` = c(1, 
NA, 1, NA, NA, NA, NA), `the holiday` = c(NA, NA, NA, 1, NA, 
1, NA), titanic = c(NA, NA, 1, 1, NA, NA, NA), contagion = c(NA, 
1, NA, 1, NA, 1, 1), `the talented mr ripley` = c(NA, 1, NA, 
NA, NA, 1, 1), `Oceans Eleven` = c(1, 1, NA, NA, NA, NA, NA), 
    `Blood Diamond` = c(NA, NA, 1, NA, 1, NA, NA)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -7L))

What I would like to do is to create a "long" dataset that shows actor and their corresponding movie by title in the following row if there was a 1 previously assigned under the movie title column.我想做的是创建一个“长”数据集,如果之前在电影标题列下分配了 1,则在下一行中按标题显示演员及其对应的电影。 Below is how i'd like to see the output.下面是我希望看到的 output。

structure(list(Actor = c("Brad Pitt", "Brad Pitt", "Matt Damon", 
"Matt Damon", "Matt Damon", "Leonardo Dicaprio", "Leonardo Dicaprio", 
"Leonardo Dicaprio", "Leonardo Dicaprio", "Kate Winslet", "Kate Winslet", 
"Kate Winslet", "Jennifer Connoley", "Jennifer Connoley", "Jude Law", 
"Jude Law", "Jude Law", "Gwenyth Paltrow", "Gwenyth Paltrow"), 
    Movie = c("Once Upon a time in Hollywood", "Oceans Eleven", 
    "The Departed", "Contagion", "The Talented MR Ripley", "The Departed", 
    "Once Upon a time in Hollywood", "Titanic", "Blood Diamond", 
    "The Holiday", "Titanic", "Contagion", "Once Upon a time in America", 
    "Blood Diamond", "The Holiday", "Contagion", "The Talented MR Ripley", 
    "Contagion", "The Talented MR Ripley")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -19L))

just use pivot_longer() and filter() from tidyverse只需使用 tidyverse 中的 pivot_longer() 和 filter()

library(tidyverse)

data %>% pivot_longer(,Actor,names_to="Movie":values_to="value") %>% dplyr:.filter(!is.na(value))

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

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