irregular splits in dataframe
try this:
x <- scan(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 + Loamy Upland 16-20 PZ", what = '', sep = '\n') Read 5 items
x
[1] "Granitic Hills 16-20 PZ" [2] "Loamy Upland 16-20 PZ" [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"
y <- strsplit(x, '/')
# now put NA is there is only one entry
y <- lapply(y, function(.line){
+ if (length(.line) == 1) .line <- c(.line, NA) # add NA + .line + })
do.call(rbind, y)
[,1] [,2] [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
On Tue, Aug 21, 2012 at 1:54 AM, Sapana Lohani <lohani.sapana at ymail.com> wrote:
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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.