[R] irregular splits in dataframe

arun smartpink111 at yahoo.com
Tue Aug 21 15:00:59 CEST 2012


Hi,


Slight modification to my earlier code to make it more simple.
dat1 <- readLines(textConnection("Granitic Hills 16-20 PZ
 Loamy Upland 16-20 PZ
 Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
 Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
 Loamy Upland 16-20 PZ")) 

dat3<-strsplit(dat1,"/")

data.frame(do.call(rbind,lapply(dat3,`[`,c(1,2))))
#                            X1                          X2
#1      Granitic Hills 16-20 PZ                        <NA>
#2        Loamy Upland 16-20 PZ                        <NA>
#3  Sandy Loam Upland 12-16 PZ    Sandy Loam, Deep 12-16 PZ
#4       Loamy Upland 12-16 PZ   Sandy Loam Upland 12-16 PZ
#5        Loamy Upland 16-20 PZ                        <NA>
A.K.

----- Original Message -----
From: Sapana Lohani <lohani.sapana at ymail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Tuesday, August 21, 2012 1:54 AM
Subject: [R] irregular splits in dataframe

Hello everyone, 


My dataframe (Soil Type) looks something like this

Granitic Hills 16-20 PZ
Loamy Upland 16-20 PZ
Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
Loamy Upland 16-20 PZ

I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ??

Thank you
    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list