如何拆分 R 中的数据框列
How to split dataframe column in R
我在名为 parm_value 的数据框中有一列,我想根据字段中下划线的位置将其分为上下限两列。我一直在尝试使用 grep 和 substring 的组合,但没有成功
当前数据帧格式:
parm_value
1 30_34
2 60_64
3 65_69
4 75_79
5 90_94
所需的数据帧格式:
parm_value lower_bound upper_bound
1 30_34 30 34
2 60_64 60 64
3 65_69 65 69
4 75_79 75 79
5 90_94 90 94
我一直在尝试
dat02 <-
dat01 %>%
mutate(lower_bound = substring(parm_value, 1, grep("_", parm_value) - 1)
使用strsplit
:
library(data.table)
xmpl <- data.table(val = rep("65_45", 5))
xmpl[ , lower := sapply(strsplit(val, "_"), "[[", 1)]
xmpl[ , upper := sapply(strsplit(val, "_"), "[[", 2)]
xmpl
# val lower upper
# 1: 65_45 65 45
# 2: 65_45 65 45
# 3: 65_45 65 45
# 4: 65_45 65 45
# 5: 65_45 65 45
如果它真的很大 table 您可以通过 运行 一次 strsplit
节省运行时间,然后在定义新的 data.table
字段时调用该对象。
strsplit
returns 一个列表:
strsplit("65_45", "_")
# [[1]]
# [1] "65" "45"
sapply
调用使用子集函数 [[
遍历列表选择第 N 项,其中 N 在 sapply
中给出为 sapply(some_list, "[[", N)
。
你可以试试你的data.frame是否叫df
:
cbind(df, `colnames<-`( do.call("rbind", sapply(df[,1], strsplit, "_")), c("lower bound", "upper bound")))
# parm_value lower bound upper bound
# 1 30_34 30 34
# 2 60_64 60 64
# 3 65_69 65 69
# 4 75_79 75 79
# 5 90_94 90 94
您还可以使用 splitstackshape
中的 cSplit
library(splitstackshape)
out = cbind(dat, setnames(cSplit(dat, "parm_value", "_", fixed = FALSE),
c("lower_bound", "upper_bound")))
#> out
# parm_value lower_bound upper_bound
#1 30_34 30 34
#2 60_64 60 64
#3 65_69 65 69
#4 75_79 75 79
#5 90_94 90 94
尝试read.table
cbind(df1[1],read.table(text= as.character(df1$parm_value), sep="_",
col.names=c('lower_bound', 'upper_bound')))
# parm_value lower_bound upper_bound
#1 30_34 30 34
#2 60_64 60 64
#3 65_69 65 69
#4 75_79 75 79
#5 90_94 90 94
或 separate
来自 tidyr
library(tidyr)
separate(df1, parm_value, into=c('lower_bound', 'upper_bound'), remove=FALSE)
# parm_value lower_bound upper_bound
#1 30_34 30 34
#2 60_64 60 64
#3 65_69 65 69
#4 75_79 75 79
#5 90_94 90 94
我在名为 parm_value 的数据框中有一列,我想根据字段中下划线的位置将其分为上下限两列。我一直在尝试使用 grep 和 substring 的组合,但没有成功
当前数据帧格式:
parm_value
1 30_34
2 60_64
3 65_69
4 75_79
5 90_94
所需的数据帧格式:
parm_value lower_bound upper_bound
1 30_34 30 34
2 60_64 60 64
3 65_69 65 69
4 75_79 75 79
5 90_94 90 94
我一直在尝试
dat02 <-
dat01 %>%
mutate(lower_bound = substring(parm_value, 1, grep("_", parm_value) - 1)
使用strsplit
:
library(data.table)
xmpl <- data.table(val = rep("65_45", 5))
xmpl[ , lower := sapply(strsplit(val, "_"), "[[", 1)]
xmpl[ , upper := sapply(strsplit(val, "_"), "[[", 2)]
xmpl
# val lower upper
# 1: 65_45 65 45
# 2: 65_45 65 45
# 3: 65_45 65 45
# 4: 65_45 65 45
# 5: 65_45 65 45
如果它真的很大 table 您可以通过 运行 一次 strsplit
节省运行时间,然后在定义新的 data.table
字段时调用该对象。
strsplit
returns 一个列表:
strsplit("65_45", "_")
# [[1]]
# [1] "65" "45"
sapply
调用使用子集函数 [[
遍历列表选择第 N 项,其中 N 在 sapply
中给出为 sapply(some_list, "[[", N)
。
你可以试试你的data.frame是否叫df
:
cbind(df, `colnames<-`( do.call("rbind", sapply(df[,1], strsplit, "_")), c("lower bound", "upper bound")))
# parm_value lower bound upper bound
# 1 30_34 30 34
# 2 60_64 60 64
# 3 65_69 65 69
# 4 75_79 75 79
# 5 90_94 90 94
您还可以使用 splitstackshape
cSplit
library(splitstackshape)
out = cbind(dat, setnames(cSplit(dat, "parm_value", "_", fixed = FALSE),
c("lower_bound", "upper_bound")))
#> out
# parm_value lower_bound upper_bound
#1 30_34 30 34
#2 60_64 60 64
#3 65_69 65 69
#4 75_79 75 79
#5 90_94 90 94
尝试read.table
cbind(df1[1],read.table(text= as.character(df1$parm_value), sep="_",
col.names=c('lower_bound', 'upper_bound')))
# parm_value lower_bound upper_bound
#1 30_34 30 34
#2 60_64 60 64
#3 65_69 65 69
#4 75_79 75 79
#5 90_94 90 94
或 separate
来自 tidyr
library(tidyr)
separate(df1, parm_value, into=c('lower_bound', 'upper_bound'), remove=FALSE)
# parm_value lower_bound upper_bound
#1 30_34 30 34
#2 60_64 60 64
#3 65_69 65 69
#4 75_79 75 79
#5 90_94 90 94