[R] replace text in table R?

David Winsemius dwinsemius at comcast.net
Thu Mar 10 00:50:37 CET 2016


> On Mar 8, 2016, at 1:48 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> 
> Thanks for the complete reproducible example.
> 
> Here's one way to approach the problem; there are many others.
> 
> recodeDat <- function(x, invals, outvals) {
> x <- as.character(x)
> invals <- as.character(invals)
> outvals <- as.character(outvals)
> # a loop is the most understandable approach
> for(i in seq_along(invals)) {
> x[x == invals[i]] <- outvals[i]
> }
> # I would change the zeros to NA values
> x[x == "0"] <- NA
> 
> factor(x, levels=sort(unique(outvals)))
> }
> 
> dat1.recode <- dat1
> dat1.recode[, 3:ncol(dat1.recode)] <- apply(dat1.recode[,
> 3:ncol(dat1.recode)], 2, recodeDat, invals=dat2[,1], outvals=dat2[,2])

Here's an lapply approach that gives the same result (complete with the misspelling of 'assigned':

dat1[3:5] <- lapply(dat1[3:5], function(x) dat2$To.be.assinged[ match( x, dat2$site) ])
dat1

`match` is designed for generating selection vectors and does not need preceding calls to as.character for factors.

--
Best;
David.
> 
> On Tue, Mar 8, 2016 at 4:26 PM, Marna Wagley <marna.wagley at gmail.com> wrote:
>> 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
>> 
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org
> 
> ______________________________________________
> R-help at 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list