Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use dplyr to replace values on select columns

Tags:

r

dplyr

I am trying to use the replace() function in dplyr to clean my data. I want to run it on all the columns except one. If I use a select() statement before I lose my character identifiers. I am looking for something like this

newdata<-data %>% replace(((.)>1000),0)

But with an exception

newdata<-data %>% replace(((-StoreID)>1000),0)
like image 407
user2502836 Avatar asked Oct 17 '25 08:10

user2502836


1 Answers

Since you didn't provide a reproducible example, here's how it would work on the iris dataset:

iris %>% mutate_each(funs(replace(., . > 5, NA)), -Species)

We use mutate_each() to replace by NA the values greater than 5 in all columns except Species


For your example it would be something like:

data %>% mutate_each(funs(replace(., . > 1000, 0)), -StoreID)
like image 146
Steven Beaupré Avatar answered Oct 19 '25 21:10

Steven Beaupré



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!