收集多個列集合的問題已在這裏解決:Gather multiple sets of columns,但在我的情況下,列不是唯一的。收集重複的列集合到單個列中
我有以下數據:
input <- data.frame(
id = 1:2,
question = c("a", "b"),
points = 0,
max_points = c(3, 5),
question = c("c", "d"),
points = c(0, 20),
max_points = c(5, 20),
check.names = F,
stringsAsFactors = F
)
input
#> id question points max_points question points max_points
#> 1 1 a 0 3 c 0 5
#> 2 2 b 0 5 d 20 20
第一列是一個id,然後我有許多重複列(原始數據集具有133列):
- 標識符問題
- 分數
- 最大分數
我想用這種結構落得:
expected <- data.frame(
id = c(1, 2, 1, 2),
question = letters[1:4],
points = c(0, 0, 0, 20),
max_points = c(3, 5, 5, 20),
stringsAsFactors = F
)
expected
#> id question points max_points
#> 1 1 a 0 3
#> 2 2 b 0 5
#> 3 1 c 0 5
#> 4 2 d 20 20
我已經試過幾件事情:
tidyr::gather(input, key, val, -id)
reshape2::melt(input, id.vars = "id")
兩個不能提供所需的輸出。此外,如果列數多於此處顯示的列數,gather
不再適用,因爲有太多重複的列。
作爲一種變通方法我試過這樣:
# add numbers to make col headers "unique"
names(input) <- c("id", paste0(1:(length(names(input)) - 1), names(input)[-1]))
# gather, remove number, spread
input %>%
gather(key, val, -id) %>%
mutate(key = stringr::str_replace_all(key, "[:digit:]", "")) %>%
spread(key, val)
它給出了一個錯誤:Duplicate identifiers for rows (3, 9), (4, 10), (1, 7), (2, 8)
已經在這裏討論這個問題:Unexpected behavior with tidyr,但我不知道爲什麼/我應該如何添加其他標識符。這很可能不是主要問題,因爲我可能應該以不同的方式處理整個事情。
我該如何解決我的問題,最好用tidyr
或base?我不知道如何使用data.table
,但如果有簡單的解決方案,我也會解決這個問題。
所有你的問題,max_points和點列實際命名爲相同的東西? –
也許'rbind(input [,c(1,2:4)],input [,c(1,5:7)])'? – zx8754
@MikeyMike是的。 –