将列中以逗号分隔的字符串拆分为单独的行

将列中以逗号分隔的字符串拆分为单独的行

我有一个数据框,如下所示:

data.frame(director = c("Aaron Blaise,Bob Walker", "Akira Kurosawa", 
                        "Alan J. Pakula", "Alan Parker", "Alejandro Amenabar", "Alejandro Gonzalez Inarritu", 
                        "Alejandro Gonzalez Inarritu,Benicio Del Toro", "Alejandro González Iñárritu", 
                        "Alex Proyas", "Alexander Hall", "Alfonso Cuaron", "Alfred Hitchcock", 
                        "Anatole Litvak", "Andrew Adamson,Marilyn Fox", "Andrew Dominik", 
                        "Andrew Stanton", "Andrew Stanton,Lee Unkrich", "Angelina Jolie,John Stevenson", 
                        "Anne Fontaine", "Anthony Harvey"), AB = c('A', 'B', 'A', 'A', 'B', 'B', 'B', 'A', 'B', 'A', 'B', 'A'
                        , 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'A'))

如您所见,director列中的某些条目是由逗号分隔的多个名称。我想将这些条目拆分为单独的行,同时保持另一列的值。例如,上面数据框中的第一行应该分成两行,director列中各有一个名称,列中有“A” AB


蓝山帝景
浏览 1123回答 3
3回答

郎朗坤

命名原始data.frame v,我们有:> s <- strsplit(as.character(v$director), ',')> data.frame(director=unlist(s), AB=rep(v$AB, sapply(s, FUN=length)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; director AB1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Aaron Blaise&nbsp; A2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bob Walker&nbsp; A3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Akira Kurosawa&nbsp; B4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Alan J. Pakula&nbsp; A5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Alan Parker&nbsp; A6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Alejandro Amenabar&nbsp; B7&nbsp; Alejandro Gonzalez Inarritu&nbsp; B8&nbsp; Alejandro Gonzalez Inarritu&nbsp; B9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Benicio Del Toro&nbsp; B10 Alejandro González Iñárritu&nbsp; A11&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Alex Proyas&nbsp; B12&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Alexander Hall&nbsp; A13&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Alfonso Cuaron&nbsp; B14&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Alfred Hitchcock&nbsp; A15&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Anatole Litvak&nbsp; A16&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Andrew Adamson&nbsp; B17&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Marilyn Fox&nbsp; B18&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Andrew Dominik&nbsp; B19&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Andrew Stanton&nbsp; B20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Andrew Stanton&nbsp; B21&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lee Unkrich&nbsp; B22&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Angelina Jolie&nbsp; B23&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; John Stevenson&nbsp; B24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Anne Fontaine&nbsp; B25&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Anthony Harvey&nbsp; A注意使用rep构建新的AB列。这里,sapply返回每个原始行中的名称数。
打开App,查看更多内容
随时随地看视频慕课网APP