简体   繁体   English

如何将单个列中的内容拆分为R中的两个单独列?

[英]How to split contents in a single column into two separate columns in R?

I have a column in my dataframe: 我的数据框中有一列:

Colname
20151102
19920311
20130204
>=70
60-69
20-29

I wish to split this column into two columns like: 我希望将此列拆分为两列,如:

Col1         Col2
20151102
19920311
20130204
            >=70
            60-69
            20-29

How can I achieve this result? 我怎样才能达到这个效果?

One possible solution, the idea is to use extract from tidyr . 一种可能的解决方案,想法是使用来自tidyr extract Note that the delimiter I choose (the dot) must not appear in your initial data.frame . 请注意,我选择的分隔符(点)不得出现在初始data.frame

library(magrittr)
library(tidyr)

df$colname = df$colname %>% 
             grepl("[>=|-]+", .) %>% 
             ifelse(paste0(".", df$colname), paste0(df$colname, ".")) 

extract(df, colname, c("col1","col2"), "(.*)\\.(.*)")
#     col1  col2
#1  222222      
#2 1111111      
#3          >=70
#4         60-69
#5         20-29

Data: 数据:

df = data.frame(colname=c("222222","1111111",">=70","60-69","20-29"))

Without the need of any package: 无需任何包装:

df[,c("Col1", "Col2")] <- ""

isnum <- suppressWarnings(!is.na(as.numeric(df$colname)))

df$Col1[isnum] <- df$colname[isnum]
df$Col2[!isnum] <- df$colname[!isnum]

df <- df[,!(names(df) %in% "colname")]

Data: 数据:

df = data.frame(colname=c("20151102","19920311","20130204",">=70","60-69","20-29"), stringsAsFactors=FALSE)

Here is a single statement solution. 这是一个单一的声明解决方案。 read.pattern captures the two field types separately in the parts of the regular expression surrounded by parentheses. read.pattern在括号括起的正则表达式的部分中分别捕获两个字段类型。 ( format can be omitted if the Colname column is already of class "character" . Also, if it were desired to have the first column numeric then omit the colClasses argument.) (如果Colname列已经是"character"类,则可以省略format 。另外,如果希望第一列是数字,则省略colClasses参数。)

library(gsubfn)
read.pattern(text = format(DF$Colname), pattern = "(^\\d+$)|(.*)", 
                   col.names = c("Col1", "Col2"), colClasses = "character")

giving: 赠送:

      col1     col2
1 20151102         
2 19920311         
3 20130204         
4          >=70    
5          60-69   
6          20-29 

Note: Here is a visualization of the regular expression used: 注意:以下是使用的正则表达式的可视化:

(^\d+$)|(.*)

正则表达式可视化

Debuggex Demo Debuggex演示

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

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