R: Split character column and create two new ones

后端 未结 4 1831
清歌不尽
清歌不尽 2021-01-25 14:37

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
          


        
相关标签:
4条回答
  • 2021-01-25 14:51

    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   
    
    0 讨论(0)
  • 2021-01-25 15:03
    # 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)
    
    0 讨论(0)
  • 2021-01-25 15:06

    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       
    
    0 讨论(0)
  • 2021-01-25 15:13

    with tidyr library:

    library(tidyr)
    df <- as.data.frame(rbind(a,b,c), stringsAsFactors=F)
    separate(df, V1, c("name", "age"),sep = ",")
    
    0 讨论(0)
提交回复
热议问题