Hi R Users, I have been struggling to replace texts in a table by new text. but it seems crazy as of I am doing it manually in R. I have very big files and some of the text has to be replaced by another class based on another file if the name corresponds. I was able to perform following example but it should be easier if there is a loop. Any suggestions on making a loop for this example?
Here is the example how I did. I want to assign class into dat1 based on dat2 table. dat1<-structure(list(ID = structure(1:8, .Label = c("X127", "X128", "X129", "X130", "X131", "X132", "X133", "X134"), class = "factor"), Name = structure(1:8, .Label = c("Site1", "Site2", "Site3", "Site4", "Site5", "Site6", "Site7", "Site8"), class = "factor"), Time1 = structure(c(4L, 2L, 3L, 5L, 1L, 1L, 4L, 5L), .Label = c("0", "GT", "R", "Tr", "W2"), class = "factor"), Time2 = structure(c(2L, 1L, 4L, 2L, 1L, 3L, 4L, 1L), .Label = c("0", "GT", "MA", "UA"), class = "factor"), Time3 = structure(c(5L, 1L, 4L, 4L, 2L, 3L, 3L, 1L), .Label = c("0", "GT", "R", "Tr", "Y7" ), class = "factor")), .Names = c("ID", "Name", "Time1", "Time2", "Time3"), class = "data.frame", row.names = c(NA, -8L )) dat1 dat2<-structure(list(site = structure(c(4L, 2L, 5L, 3L, 6L, 7L, 8L, 1L), .Label = c("GT", "MA", "R", "Tr", "UA", "W1", "W2", "Y7" ), class = "factor"), To.be.assinged = structure(c(1L, 2L, 3L, 1L, 4L, 4L, 1L, 2L), .Label = c("A", "B", "C", "D"), class = "factor")), .Names = c("site", "To.be.assinged"), class = "data.frame", row.names = c(NA, -8L )) dat2 A2 <- as.data.frame(lapply(dat1,function(x) if(is.character(x)|is.factor(x)) gsub("Tr","A",x) else x)) A3 <- as.data.frame(lapply(A2,function(x) if(is.character(x)|is.factor(x)) gsub("MA","B",x) else x)) A4 <- as.data.frame(lapply(A3,function(x) if(is.character(x)|is.factor(x)) gsub("UA","C",x) else x)) A5 <- as.data.frame(lapply(A4,function(x) if(is.character(x)|is.factor(x)) gsub("R","A",x) else x)) A6 <- as.data.frame(lapply(A5,function(x) if(is.character(x)|is.factor(x)) gsub("W1","D",x) else x)) A7 <- as.data.frame(lapply(A6,function(x) if(is.character(x)|is.factor(x)) gsub("W2","D",x) else x)) A8 <- as.data.frame(lapply(A7,function(x) if(is.character(x)|is.factor(x)) gsub("Y7","A",x) else x)) A9 <- as.data.frame(lapply(A8,function(x) if(is.character(x)|is.factor(x)) gsub("GT","B",x) else x)) A9 Your help is highly appreciated. Thanks [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.