将重复的列折叠成行 [英] Collapse repeated columns into rows
问题描述
我有一个从API提取的数据框。清洗后,它看起来像这样:
I have a dataframe that I pulled in from an API. After some cleaning it looks something like this:
Title Year Rating Title Year Rating Title Year Rating
Movie 1 1997 6.7 Movie 2 1987 8.2 Movie 3 2009 7.1
列标题重复,在这种情况下,单行包含3个单独的条目。
The column headers repeat, and in this case a single row contains 3 separate entries.
我将如何重塑它,以便最终得到3列(标题,年份,评分)和3行(电影1,电影2,电影3)?
How would I reshape this so that I end up with 3 columns (Title, Year, Rating) and 3 rows (Movie 1, Movie 2, Movie 3)?
最简单的方法是什么?
推荐答案
将输入data.frame转换为列表,然后根据其公共列名将各列分为几组。然后取消列出每组列,以在每组中产生一个列,然后转换回data.frame。 (如果 DF
中有多行,这也适用。)
Convert the input data.frame to a list and split the columns into groups according to their common column names. Then unlist each group of columns to produce a single column in each group and convert back to a data.frame. (This also works if there is more than one row in DF
.)
as.data.frame(lapply(split(as.list(DF), names(DF)), unlist))
给予:
Rating Title Year
1 6.7 Movie1 1997
2 8.2 Movie2 1987
3 7.1 Movie3 2009
注意:此输入:
Lines <- "Title Year Rating Title Year Rating Title Year Rating
Movie1 1997 6.7 Movie2 1987 8.2 Movie3 2009 7.1"
DF <- read.table(text = Lines, header = TRUE, check.names = FALSE, as.is = TRUE)
这篇关于将重复的列折叠成行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!