On Aug 21, 2012, at 3:24 PM, Sapana Lohani wrote:

Hello,



Re framing my previous question, my dataframe (Soil Type) looks something like below. I want the code to find "/" first and split the string into 2 or 3 depending on the number of "/". I don't want to specify the number of columns after split. How can I do that ?


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 / Limy Slopes 16-20 PZ

Loamy Upland 16-20 PZ

When there is just one type, I want "NA" in the second / third column.

 inp <- read.table(text="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 / Limy Slopes 16-20 PZ
 Loamy Upland 16-20 PZ", sep="/", fill = TRUE)
 is.na(inp) = inp==""
 inp
#-----------------
V1 V2 V3 1 Granitic Hills 16-20 PZ <NA> <NA> 2 Loamy Upland 16-20 PZ <NA> <NA> 3 Sandy Loam Upland 12-16 PZ Sandy Loam, Deep 12-16 PZ <NA> 4 Loamy Upland 12-16 PZ Sandy Loam Upland 12-16 PZ Limy Slopes 16-20 PZ 5 Loamy Upland 16-20 PZ <NA> <NA>

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
R-help@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.

Reply via email to