简体   繁体   English

R将一行多列中的变量分成多行

[英]R separate variables in one row of multiple columns into multiple rows

I have a data frame with 563 columns.我有一个包含 563 列的数据框。

Each column currently has one row containing multiple values of different length.每列当前有一行包含多个不同长度的值。

For example:例如:

col_1            col_2               col_3       ...   col_563  
c("1","2","3")   c("1","2","3"...)   c("1","2")        c("1","2","3"...)

I want to separate the value in the columns into multiple rows:我想将列中的值分成多行:

col_1     col_2   col_3   ...   col_563  
"1"       "1"     "1"           "1"
"2"       "2"     "2"           "2"
"3"       "3"                   "3"
          "4"                   "4" 
          "5"

I have tried:我努力了:

separate_rows(df, "row1":"row563", convert = TRUE)

But I got the error:但我得到了错误:

Error in `fn()`:
! In row 1, can't recycle input of size 778 to size 124.

Does anyone know how I should proceed?有谁知道我应该如何进行?

In Base R:在基础 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

You could do:你可以这样做:

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

where the data looks as:数据如下所示:

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.

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