我有以下数据框df
,以下是数据框中df[1]
的第一列:
Well and Depth
Black Peak 1000
Black Peak 1001
Black Peak 1002
Black Peak 1003
RStudio目前将此列视为一系列因素,但我想将其拆分为两个数据框列/向量,一个用文本作为字符串/ char,另一个用数字作为数值。所以它看起来像这样:
Well Depth
"Black Peak" 1000
"Black Peak" 1001
"Black Peak" 1002
"Black Peak" 1003
将绘制深度数字。
答案 0 :(得分:2)
你可以试试这个:
df$Well <- sub("(^.*) [0-9]+$", "\\1", df$`Well and Depth`)
df$Depth <- as.numeric(sub(".* ([0-9]+$)", "\\1", df$`Well and Depth`))
数据:
structure(list(`Well and Depth` = structure(1:4, .Label = c("Black Peak 1000",
"Black Peak 1001", "Black Peak 1002", "Black Peak 1003"), class = "factor")), .Names = "Well and Depth", row.names = c(NA,
-4L), class = "data.frame")
答案 1 :(得分:1)
HERE=data.frame(WELL=character(),DEPTH=numeric())
strcapture("(.*)\\s(\\d+)$",as.character(df[,1]),HERE)
WELL DEPTH
1 Black Peak 1000
2 Black Peak 1001
3 Black Peak 1002
4 Black Peak 1003
答案 2 :(得分:0)
我们可以使用separate
tidyr
library(tidyr)
separate(df1, `Well and Depth`, into = c("Well", "Depth"), "\\s+(?=[0-9])")
# Well Depth
#1 Black Peak 1000
#2 Black Peak 1001
#3 Black Peak 1002
#4 Black Peak 1003