简体   繁体   English

没有“分隔符”时,将R中的一列拆分为多列

[英]Splitting a column into multiple columns in R, when there is no “separator”

Suppose I have a data frame with eight digit numbers, each corresponding to one 假设我有一个包含八位数字的数据框,每个数字对应一个

x <- data.frame(y = c(12345678, 87654321))

I want 8 columns such that each column corresponds to one number ie 我想要8列,以便每一列对应一个数字,即

   y1   y2   y3   y4   y5   y6   y7   y8
    1    2    3    4    5    6    7    8     
    8    7    6    5    4    3    2    1   

(The columns don't have to be named y1 to y8, I'm just naming them that way for illustrative purposes) (这些列不必命名为y1到y8,我只是以说明的方式命名它们)

Normally in R, I would use separate , but here there is no clear separator. 通常在R中,我将使用separate ,但是这里没有明确的分隔符。 How can I do this? 我怎样才能做到这一点?

separate can accept column positions in the sep argument. separate可以接受sep参数中的列位置。 This acts as if there were separators after columns 1, 2, ..., 7. 这就像在第1、2,...,7列之后有分隔符一样。

library(tidyr)

separate(x, y, into = paste0("y", 1:8), sep = 1:7)

giving: 给予:

  y1 y2 y3 y4 y5 y6 y7 y8
1  1  2  3  4  5  6  7  8
2  8  7  6  5  4  3  2  1

Here is a base R option using strsplit 这是使用strsplit的基本R选项

as.data.frame(t(apply(x, 1, function(x) unlist(strsplit(as.character(x), "")))))
#  V1 V2 V3 V4 V5 V6 V7 V8
#1  1  2  3  4  5  6  7  8
#2  8  7  6  5  4  3  2  1

Or a tidyverse option tidyverse选项

library(tidyverse)
x %>%
    rowid_to_column("row") %>%
    mutate(y = strsplit(as.character(y), "")) %>%
    unnest() %>%
    group_by(row) %>%
    mutate(var = paste0("y", 1:n())) %>%
    spread(var, y) %>%
    ungroup() %>%
    select(-row)
#  y1    y2    y3    y4    y5    y6    y7    y8
#  <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#1 1     2     3     4     5     6     7     8
#2 8     7     6     5     4     3     2     1

Another R base alternative 另一个R base替代

> data.frame(do.call(rbind, strsplit(as.character(x$y), "")))
  X1 X2 X3 X4 X5 X6 X7 X8
1  1  2  3  4  5  6  7  8
2  8  7  6  5  4  3  2  1

Solution from stringr stringr解决方案

data.frame(stringr::str_split_fixed(x$y, "", 8))
  X1 X2 X3 X4 X5 X6 X7 X8
1  1  2  3  4  5  6  7  8
2  8  7  6  5  4  3  2  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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