[R] how to replace a text in a table by another text in R?

arun smartpink111 at yahoo.com
Wed Dec 4 14:33:12 CET 2013


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



More information about the R-help mailing list