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]]
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.
Sapana Lohani <lohani.sapana@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 shownbelow. 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) andpercentage(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]]
?strsplit -- Bert On Thu, Aug 16, 2012 at 11:41 AM, Sapana Lohani <lohani.sapana at ymail.com> wrote:> 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
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.
HI, Slightly different way to do 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) ")) dat2<-data.frame(dat1) dat3<-data.frame(Name=dat2[2:5,],slope=dat2[2:5,],percentage=dat2[2:5,]) dat3$Name<-gsub("(.*Complex)\\s-\\s(.*slope).*(\\(.*\\))","\\1",dat3$Name) dat3$slope<-gsub("(.*Complex)\\s-\\s(.*slope).*(\\(.*\\))","\\2",dat3$slope) dat3$percentage<-gsub("(.*Complex)\\s-\\s(.*slope).*(\\(.*\\))","\\3",dat3$percentage) ? ?dat3 #???????????????????????????????? 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. ________________________________ From: Sapana Lohani <lohani.sapana at ymail.com> To: arun <smartpink111 at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Thursday, August 16, 2012 7:08 PM Subject: Re: [R] How to extract from a column in a table? Thank you :) ________________________________ From: arun <smartpink111 at yahoo.com> To: Sapana Lohani <lohani.sapana at ymail.com> Cc: R help <r-help at r-project.org> Sent: Thursday, August 16, 2012 1:06 PM Subject: Re: [R] How to extract from a column in a table? 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.
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]]
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.