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
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
# 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)
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
with tidyr library:
library(tidyr)
df <- as.data.frame(rbind(a,b,c), stringsAsFactors=F)
separate(df, V1, c("name", "age"),sep = ",")