[英]Combining multiple rows for one ID into one row with multiple columns based on 2 different variables in R
[英]R separate variables in one row of multiple columns into multiple rows
我有一个包含 563 列的数据框。
每列当前有一行包含多个不同长度的值。
例如:
col_1 col_2 col_3 ... col_563
c("1","2","3") c("1","2","3"...) c("1","2") c("1","2","3"...)
我想将列中的值分成多行:
col_1 col_2 col_3 ... col_563
"1" "1" "1" "1"
"2" "2" "2" "2"
"3" "3" "3"
"4" "4"
"5"
我努力了:
separate_rows(df, "row1":"row563", convert = TRUE)
但我得到了错误:
Error in `fn()`:
! In row 1, can't recycle input of size 778 to size 124.
有谁知道我应该如何进行?
在基础 R 中:
a <- unlist(df, FALSE)
data.frame(lapply(a, `length<-`, max(lengths(a))))
col_1 col_2 col3 col_5
1 1 1 1 1
2 2 2 2 2
3 3 3 NA 3
4 NA 4 NA 4
5 NA 5 NA NA
你可以这样做:
library(tidyverse)
flatten(df) %>%
map_dfc(`length<-`, max(length(.)))
# A tibble: 4 x 4
col_1 col_2 col3 col_5
<int> <int> <int> <int>
1 1 1 1 1
2 2 2 2 2
3 3 3 NA 3
4 NA 4 NA 4
数据如下所示:
df <- structure(list(col_1 = list(1:3), col_2 = list(1:5), col3 = list(
1:2), col_5 = list(1:4)), row.names = c(NA, -1L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.