Skip to content

How to extract from a column in a table?

10 messages · arun, Jean V Adams, Bert Gunter +3 more

#
Hi,

Try this:

dat1<-readLines(textConnection("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)
"))
dat3<-data.frame(dat2[-1,])
colnames(dat3)<-"Name"
col2<-gsub(".*-\\s(.*slope).*","\\1",dat3$Name)
?col3<-gsub(".*(\\(.*\\))","\\1",dat3$Name)
?col1<-gsub("(.*Complex).*","\\1",dat3$Name)
?dat3New<-data.frame(col1,col2,col3)
?dat3New
???????????????????????????????? col1??????????? col2????????? col3
1????????? Budlamp-Woodcutter Complex 15 to 60% slope??? (60/25/15)
2????????? Budlamp-Woodcutter Complex 15 to 60% slope??? (60/25/15)
3 Terrarossa-Blacktail-Pyeatt Complex? 1 to 40% slope (40/35/15/10)
4 Terrarossa-Blacktail-Pyeatt Complex? 1 to 40% slope (40/35/15/10)

A.K.



----- Original Message -----
From: Sapana Lohani <lohani.sapana at ymail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Thursday, August 16, 2012 2:41 PM
Subject: [R] How to extract from a column in a table?

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.
#
Hi,

Forgot the last part.

colnames(dat3New)<-c("name","slope","percentage")
?dat3New
???????????????????????????????? name?????????? slope??? percentage
1????????? Budlamp-Woodcutter Complex 15 to 60% slope??? (60/25/15)
2????????? Budlamp-Woodcutter Complex 15 to 60% slope??? (60/25/15)
3 Terrarossa-Blacktail-Pyeatt Complex? 1 to 40% slope (40/35/15/10)
4 Terrarossa-Blacktail-Pyeatt Complex? 1 to 40% slope (40/35/15/10)
A.K.



----- Original Message -----
From: Sapana Lohani <lohani.sapana at ymail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Thursday, August 16, 2012 2:41 PM
Subject: [R] How to extract from a column in a table?

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.
#
?strsplit

-- Bert
On Thu, Aug 16, 2012 at 11:41 AM, Sapana Lohani <lohani.sapana at ymail.com> wrote:

  
    
#
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:
#
Hello,

Try the following.


x <- c("slope (60/25/15)",
"slope (90/10)",
"slope (40/35/15/10)",
"slope (40/25/25/10)")
dat <- data.frame(X = x)

lst <- unlist(lapply(dat, function(.x) gsub("slope \\((.*)\\)$", "\\1", 
.x)))
lst <- strsplit(lst, "/")
keep <- seq_len(max(unlist(lapply(lst, length))))
vec <- rep("0", length(keep))
result <- do.call(rbind, lapply(lst, function(.x) as.integer(c(.x, 
vec)[keep])))


Now, in your case, just substitute 'dat' by the name of your vector.
(I think this solution is too complicated but it predicts for the case 
where the input vector is in fact a list.)

Hope this helps,

Rui Barradas

Em 17-08-2012 06:05, Sapana Lohani escreveu:
#
HI,
Try this:
dat1<-data.frame(slope=c("slope (60/25/15)","slope (90/10)","slope (40/35/15/10)","slope (40/25/25/10)" ))
dat1$slope<-gsub("slope\\s+.*(\\(.*\\))","\\1",dat1$slope)
?dat1$slope<-gsub("\\((.*)\\)","\\1",dat1$slope)
dat2<-strsplit(dat1$slope,"/")
dat2[[1]][4]<-0
dat2[[2]][3:4]<-0
data.frame(do.call(rbind,dat2))
#? X1 X2 X3 X4
#1 60 25 15? 0
#2 90 10? 0? 0
#3 40 35 15 10
#4 40 25 25 10



----- Original Message -----
From: Sapana Lohani <lohani.sapana at ymail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Friday, August 17, 2012 1:05 AM
Subject: [R] Unequal splits from a column

Hi I am new to R so am struggling with the commands here

I have one column in a table that looks like


slope (60/25/15) 
slope (90/10) 
slope (40/35/15/10) 
slope (40/25/25/10) 
I want to have 4 columns with just the number inside the parenthesis. when there is no number that cell can have 0. I want the output like this

60 25 15 0
90 10 0 0
40 35 15 10
40 25 25 10

Can somebody help me??
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.
#
Here's a slightly different approach without regular expressions:
"slope (40/35/15/10)","slope (40/25/25/10)" ))
maxlen-length(x))))))
X1 X2 X3 X4
1 60 25 15  0
2 90 10  0  0
3 40 35 15 10
4 40 25 25 10

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352