将一列中的文本拆分为每一行的多列 [英] Splitting a text in one column into many columns for each row
问题描述
我有以下数据集:
Class Range Value
A 6 - 8 19
B 1 - 3 14
C 5 - 16 10
D 4 - 7 5
我想将每个班级的范围分成两列.为此,我使用了 str_split_fixed
函数,如下所示:
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))
我什至尝试过:
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?
推荐答案
str_split_fixed
的输出是一个两列的matrix
(没有dimnames),当我们做一个merge
没有指定列名,它会进行交叉连接.代替 merge
,我们可以使用 cbind
或分配给两列
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)
注意:str_split
的输出是 character
类型的元素.它可能需要转换为 numeric
NOTE: The output of str_split
are elements with character
type. It may need to converted to numeric
更简单的选择是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
这篇关于将一列中的文本拆分为每一行的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!