Sapana Lohani <lohani.sap...@ymail.com> wrote on 08/16/2012 01:41:23 PM:
> 
> Hi, 
> 
> I have a table in which one column has the name of the objects as shown 
below.


You don't provide example data, so I'm not sure what you mean by a 
"table".  I will assume that you mean a data frame.


> Name 
> 
> Budlamp-Woodcutter Complex - 15 to 60% slope (60/25/15) 
> Budlamp-Woodcutter Complex - 15 to 60% slope (60/25/15) 
> Terrarossa-Blacktail-Pyeatt Complex - 1 to 40% slope (40/35/15/10) 
> Terrarossa-Blacktail-Pyeatt Complex - 1 to 40% slope (40/35/15/10) 
> 
> How can I split the single column into three columns like name 
> (Budlamp-Woodcutter Complex), slope (15 to 60% slope) and 
percentage(60/25/15)
> 
> thanks


# example data frame
mydat <- data.frame(Name = c("Budlamp-Woodcutter Complex - 15 to 60% slope 
(60/25/15)", 
        "Budlamp-Woodcutter Complex - 15 to 60% slope (60/25/15)", 
        "Terrarossa-Blacktail-Pyeatt Complex - 1 to 40% slope 
(40/35/15/10)", 
        "Terrarossa-Blacktail-Pyeatt Complex - 1 to 40% slope 
(40/35/15/10)"), x=1:4)

# find where the three splitting strings are
dash <- regexpr(" - ", mydat$Name)
leftp <- regexpr(" \\(", mydat$Name)
rightp <- regexpr("\\)", mydat$Name)

# define three new columns based on the location of the splitting strings
mydat$name <- substring(mydat$Name, 1, dash-1)
mydat$slope <- substring(mydat$Name, dash + attr(dash, "match.length"), 
leftp-1)
mydat$percentage <- substring(mydat$Name, leftp + attr(leftp, 
"match.length"), rightp-1)

mydat


        [[alternative HTML version deleted]]

______________________________________________
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