按空间拆分列
DATA <- data.frame("V1" = c("SCORE : 9.931 5.092",
"SCORE : 6.00007 15.1248",
"SCORE : 1.0002 12.987532",
"SCORE : 3.1 3.98532"))
WANT <- data.frame("VAR1" = c(9.931, 6.00007, 1.0002, 3.1),
'VAR2' = c(5.092, 15.1248, 12.987532, 3.98532))
我所拥有的是像“数据”中所示输入的学生考试成绩数据,但我希望将其拆分,以便我所拥有的就像“想要”框架中显示的那样,其中第一个数字位于“VAR1”中,而第二个数字在“VAR2”中,忽略空格
我的尝试:
DATA[, c("VAR1", "VAR2") := trimws(V1, whitespace = ".*:\\s+")]
DATA <- data.frame("V1" = c("SCORE : 9.931 5.092",
"SCORE : 6.00007 15.1248",
"SCORE : 1.0002 12.987532",
"SCORE : 3.1 3.98532"))
WANT <- data.frame("VAR1" = c(9.931, 6.00007, 1.0002, 3.1),
'VAR2' = c(5.092, 15.1248, 12.987532, 3.98532))
What I have is student test score data that was entered like shown in 'DATA' but I wish to split it so that what I have is like what is shown in the 'WANT' frame where the first number is in 'VAR1' and the second number is in 'VAR2' ignoring the spaces
MY ATTEMPT:
DATA[, c("VAR1", "VAR2") := trimws(V1, whitespace = ".*:\\s+")]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
trimws
删除前缀子字符串,并使用read.table
以sep
作为默认空间来读取列,以返回两列数据.frame
inbase R
或者可以使用
extract
fromtidyr
如果我们想要
data.table
,使用同样的方法可以在删除后用fread
读取前缀子字符串或者使用
fread
中的select
选项或者读取为四列,和子集
或者可以使用
tstrsplit
We can remove the prefix substring with
trimws
and read the column usingread.table
withsep
as default space to return two columndata.frame
inbase R
Or may use
extract
fromtidyr
If we want
data.table
, using the same method can read withfread
after removing the prefix substringOr use the
select
option infread
Or read as four columns, and subset
Or can use
tstrsplit