Skip to content
Prev 303536 / 398503 Next

How to extract from a column in a table?

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: