R: Split character column and create two new ones

拈花ヽ惹草 提交于 2019-12-20 04:55:41

问题


R users

I have a data frame similar to this:

a <- c("John, 3 years") 
b <- c("Mokobe, 11 years")
c <- c("Ivan")
df <- rbind(a,b,c)
df
  [,1]              
a "John, 3 years"   
b "Mokobe, 11 years"
c "Ivan" 

Which function should I use to split the column after comma to get:

df
[,1]    [,2]
John    3 years
Mokobe  11 years
Ivan    NA

回答1:


we can do a strsplit by the delimiter , and then rbind the list elements after padding with NA at the end to make length same for each list element

lst <- strsplit(df[,1], ", ")
do.call(rbind, lapply(lst, `length<-`, max(lengths(lst))))
#   [,1]     [,2]      
#a "John"   "3 years" 
#b "Mokobe" "11 years"
#c "Ivan"   NA       



回答2:


with tidyr library:

library(tidyr)
df <- as.data.frame(rbind(a,b,c), stringsAsFactors=F)
separate(df, V1, c("name", "age"),sep = ",")



回答3:


Just read the data directly by read.csv with fill=TRUE and header=FALSE. you can decide to change it to matrix by as.matrix()

    read.csv(text=df,fill=T,header=F,na.strings = "")
      V1        V2
1   John   3 years
2 Mokobe  11 years
3   Ivan      <NA>   

Turning to a matrix. Although not necessary

as.matrix(read.csv(text=df,fill=1,h=0,na.strings = ""))
     V1       V2         
[1,] "John"   " 3 years" 
[2,] "Mokobe" " 11 years"
[3,] "Ivan"   NA   



回答4:


# This should work
library(stringr)

a <- c("John, 3 years") 
b <- c("Mokobe, 11 years")
c <- c("Ivan")
df<- rbind(a,b,c)

df<- str_split_fixed(df, ",", 2)


来源:https://stackoverflow.com/questions/51794944/r-split-character-column-and-create-two-new-ones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!