Knight
Knight

Reputation: 363

Replacing all the cells with 0 except few specified values R

I have a data frame(df) which looks like,

  Id     Name       Activity.
  1      ABC        a;sldkj kkkdk
  2      two         llsjdfljs
  3      three       case one
  4      four        randshsjl lskjd
  5      five        case seven
  8      eight        sllslsll.asdhf
  9      nine         case ten

I want the replace all values in Activity to '0' except 'case one', 'case seven', 'case ten'

  Id     Name       Activity.
  1      ABC        0
  2      two        0
  3      three      case one
  4      four       0
  5      five       case seven
  8      eight      0
  9      nine       case ten

I tried,

    df2 <- subset(df, df$`Activity.!='case one'|df$`Activity.`!='case two'|df$`Activity.`!='case three')
    df2$Activity <- 0
    df <- merge(df,df2)

which becomes lengthy in code, so I was looking for some better solution

Upvotes: 3

Views: 918

Answers (2)

akrun
akrun

Reputation: 887163

We can use grep

df1$"Activity."[!grepl('^case', df1$"Activity.")] <- 0
df1
#   Id  Name  Activity.
#1  1   ABC          0
#2  2   two          0
#3  3 three   case one
#4  4  four          0
#5  5  five case seven
#6  8 eight          0
#7  9  nine   case ten

If there are more case and we want to only grep on the above mentioned 'case'

df1$"Activity."[grepl('^case\\s+(one|seven|ten)', df1$"Activity.")] <- 0

If we are using data.table, a faster and efficient method would be to set 'Activity.' as the 'key' column after converting to 'data.table' (setDT(df1,..), and then assign (:=), the values in 'Activity' to '0' that are not 'case one', or 'case seven', ...

library(data.table)
setDT(df1, key='Activity.')[!(paste('case', c('one', 'seven', 
                    'ten'))), Activity. := '0']
df1
#   Id  Name  Activity.
#1:  1   ABC          0
#2:  3 three   case one
#3:  5  five case seven
#4:  9  nine   case ten
#5:  2   two          0
#6:  4  four          0
#7:  8 eight          0

Upvotes: 4

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521409

Try this solution:

v <- paste("case", c("one", "seven", "ten"))
df[!(df$Activity %in% v), ]

Upvotes: 2

Related Questions