[R] How to extract from a column in a table?

Rui Barradas ruipbarradas at sapo.pt
Thu Aug 16 22:21:49 CEST 2012


Hello,

Try the following.

x <- 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)" )

lapply(strsplit(x, " - "), function(.x){
     s1 <- .x[1]
     s2 <- gsub(" \\(.*\\)$", "", .x[2])
     s3 <- gsub("^.*(\\(.*\\)$)", "\\1", .x[2])
     c(s1, s2, s3)
})


If you want to have the output in the form of matrix or data.frame, 
change the above to:

xx <- lapply(...etc...)
mat <- do.call(rbind, xx)  # matrix
dat <- data.frame(mat)   # data.frame

Hope this helps,

Rui Barradas
Em 16-08-2012 19:41, Sapana Lohani escreveu:
> Hi,
>
>
> I have a table in which one column has the name of the objects as shown below.
>
>
> 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
>
> 	[[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