简体   繁体   English

将data.frame的列拆分为更多列

[英]Splitting column of a data.frame into more columns

I want to split the Out column of Test data.frame into columns separating based on blank space. 我想将Test data.frameOutdata.frame为基于空白分隔的列。 Here is my MWE. 这是我的MWE。 I tried separate function from tidyr package and strsplit function from base R but couldn't figured out the problem. 我试图separate功能从tidyr包和strsplit功能从base R但不能想出的问题。

Test <- 
  structure(list(Out = structure(1:2, .Label = c("t1* -0.4815861  0.3190424   0.2309631", 
  "t2*  0.9189246 -0.1998455   0.2499412"), class = "factor")),
   .Names = "Out", row.names = c(NA, -2L), class = "data.frame")


library(dplyr)
library(tidyr)

Test %>% separate(Out, c("A", "B", "C", "D"),  sep = " ")
Error: Values not split into 4 pieces at 1, 2

strsplit(Test$Out,  " ")
Error in strsplit(Test$Out, " ") : non-character argument

try 尝试

Test %>% separate(Out, c("A", "B", "C", "D"),  sep = "\\s+")

which allows for multiple spaces ( \\\\s+ ). 允许有多个空格( \\\\s+ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM