Kristi Glover
2013-Dec-04 04:29 UTC
[R] how to replace a text in a table by another text in R?
Hi R user, I have been struggling to add number in front of text. It mus t be easy. but I could not figure it out. example I have this data: name value central -10.91699497 western -10.16521404 upper -6.915860837 lower -6.546794281 southern -6.382722608 I want to following. I want to add number in front of text. The number are not in sequence. name value 1central -10.91699497 3western -10.16521404 2upper -6.915860837 4lower -6.546794281 5southern -6.382722608 How can we find something in a table and replace by something in R? It could be easy in R but my data is so big so that Excel does not open the file. I would really appreciate your help. [[alternative HTML version deleted]]
Hi, Use ?paste() dat1<- read.table(text="name????? value central??? -10.91699497 western??? -10.16521404 upper??? -6.915860837 lower??? -6.546794281 southern -6.382722608",sep="",header=TRUE,stringsAsFactors=FALSE) ?number1 <- c(1,3,2,4,5) dat1[,1] <- paste0(number1,dat1[,1]) A.K. On Tuesday, December 3, 2013 11:31 PM, Kristi Glover <kristi.glover at hotmail.com> wrote: Hi R user, I have been struggling to add number in front of text. It mus t be easy. but I could not figure it out. example I have this data: name? ? ? value central? ? -10.91699497 western? ? -10.16521404 upper? ? -6.915860837 lower? ? -6.546794281 southern -6.382722608 I want to following. I want to add number in front of text. The number are not in sequence. name? ? ? ? value 1central? ? -10.91699497 3western? ? -10.16521404 2upper? ? ? ? -6.915860837 4lower? ? ? ? -6.546794281 5southern? ? -6.382722608 How can we find something in a table and replace by something in R? It could be easy in R but my data is so big so that Excel does not open the file. I would really appreciate your help. ??? ??? ??? ? ??? ??? ? ??? [[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 Kristi, No problem. Try: library(stringr) dat1[,1] <- factor(str_trim(dat1[,1]),labels=paste0(c(4,2,6,7,1,5,3),? levels(factor(str_trim(dat1[,1])))) ) ?dat1[,1] # [1] 1south???? 2north???? 3west????? 4east????? 1south???? 2north??? # [7] 5southeast 4east????? 6northeast 7northwest 1south???? 4east???? #[13] 3west????? 6northeast 1south??? Levels: 4east 2north 6northeast 7northwest 1south 5southeast 3west A.K. On Wednesday, December 4, 2013 2:49 AM, Kristi Glover <kristi.glover at hotmail.com> wrote: Hi Arun, Thank you so much for the message. I am very sorry for bothering you. I have attached an example of what I have and what I wanted (dat2). I have about 2500 rows. but here is only a portion of the data. Thanks once again for your help, Arun. Thanks dat1<-structure(list(class = structure(c(5L, 2L, 8L, 1L, 5L, 2L, 7L, 1L, 3L, 4L, 5L, 1L, 8L, 3L, 6L), .Label = c("east", "north", "northeast", "northwest", "south", "south ", "southeast", "west" ), class = "factor"), value = c(0.241084211, 0.95445978, 0.144307515, 0.43654453, 0.92909045, 0.7824522, 0.649447435, 0.639809128, 0.645497554, 0.761954955, 0.399209118, 0.335850182, 0.751427831, 0.548944398, 0.406230898)), .Names = c("class", "value"), class = "data.frame", row.names = c(NA, -15L)) output dat2<-structure(list(class = structure(c(1L, 3L, 4L, 5L, 1L, 3L, 6L, 5L, 7L, 8L, 1L, 5L, 4L, 7L, 2L), .Label = c("1south", "1south ", "2north", "3west", "4east", "5southeast", "6northeast", "7northwest" ), class = "factor"), value = c(0.241084211, 0.95445978, 0.144307515, 0.43654453, 0.92909045, 0.7824522, 0.649447435, 0.639809128, 0.645497554, 0.761954955, 0.399209118, 0.335850182, 0.751427831, 0.548944398, 0.406230898)), .Names = c("class", "value"), class = "data.frame", row.names = c(NA, -15L))