[R] Irregular splits in dataframe

David Winsemius dwinsemius at comcast.net
Wed Aug 22 06:30:34 CEST 2012


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




More information about the R-help mailing list