Splitting a text in one column into many columns for each row [duplicate]

别等时光非礼了梦想. 提交于 2019-12-02 03:41:18

问题


I have the following dataset:

Class      Range      Value
A          6 - 8      19
B          1 - 3      14
C          5 - 16     10
D          4 - 7      5

I want to split the range for each class into two columns. To do that, I used the function str_split_fixed as the following:

merge(data, str_split_fixed(data[, 2], " - ", 2))

and I even tried:

merge(data, str_split_fixed(data$Range, " - ", 2))

But both of them give me the following results:

Class      Range      Value    V1     V2
A          6 - 8      19       6      8
B          1 - 3      14       6      8
C          5 - 16     10       6      8
D          4 - 7      5        6      8

My question is, why does it repeat the first range for the rest of the classes? Can someone help?


回答1:


The output of str_split_fixed is a two column matrix (no dimnames), and when we do a merge with out specifying the column name, it does a cross join. Instead of merge, we could use a cbind or assign to two columns

data[c('V1', 'V2')] <- str_split_fixed(data[, 2], " - ", 2)

NOTE: The output of str_split are elements with character type. It may need to converted to numeric


An easier option is separate

library(tidyverse)
data %>%
    separate(Range, into = c("V1", "V2"), convert = TRUE)
#   Class V1 V2 Value
#1     A  6  8    19
#2     B  1  3    14
#3     C  5 16    10
#4     D  4  7     5



来源:https://stackoverflow.com/questions/54326623/splitting-a-text-in-one-column-into-many-columns-for-each-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!