Amnor
Amnor

Reputation: 380

Replace various data.frame substrings with a variable in R

I have a problem with a replacement in r, I have a variable with many values that I want to replace in the substring "fin", i want to replace the values with less than 26 frecuency, here's my code:

 n.tab <- table(as.character( joined1$codigo) )
    n.many <- names( n.tab[ n.tab < 26] )
    length(n.many)#2311 NOTE: fin is made with paste of values, codigo is one of them 
    joinedAux<-joined #saving a copy
    for (j in 1:length(n.many)) {#walk through elements of n.many

        joinedAux$fin<-gsub(as.character(n.many[j]), "other", as.character(joined$fin))#substitute (not working)

    }

And it comes with this warning: "the number of items to replace is not a multiple of the length of the replacement"

It does nothing, when I watch "joinedAux$fin" it's just the same than "joined$fin". Here's an example:

Joined$codigo    Joined$fin
12               12 valid high
22               22 wrong un
23               23 wrong in
13               13 valid high
15               15 valid very high

What I need it to be after the replace:

Joined$codigo    Joined$fin
    12               other valid high #codigo in n.many 
    22               22 wrong un
    23               other wrong in #codigo in n.many 
    13               other valid high #codigo in n.many 
    15               15 valid very high

Thanks in advance!

Upvotes: 0

Views: 54

Answers (1)

jkt
jkt

Reputation: 946

If I understand your problem correctly, you will need to loop through your data.frame rather than the subset of strings.

joinedAux$fin <- sapply(1:nrow(joinedAux), function(j) if(joined1$codigo[j] %in% n.many) gsub(joined1$codigo[j],'other',joined1$codigo[j]) else joined1$codigo[j])

If you are not comfortable with sapply, a for loop will work, too:

fin <- NULL
for(j in 1:nrow(joinedAux)) fin[j] <- if(joined1$codigo[j] %in% n.many) gsub(joined1$codigo[j],'other',joined1$codigo[j]) else joined1$codigo[j])

If you are happy to avoid gsub (which doesn't work with vectors), the simplest way would probably be:

joinedAux$fin <- joined1$codigo
joinedAux$fin[which(joined1$codigo %in% n.many)] <- 'other'

Upvotes: 1

Related Questions