Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a specific character in all of the variables in data frame

Tags:

r

tidyverse

I have this data, where every cell consists of characters,

x1 <- c(100, 0, 120)
x2 <- c(0, 0, 0)
x3 <- c(110, 0, 0)
data<- data.frame(x1, x2, x3)
testdata <- lapply(data, as.character)
testdata
$`x1`
[1] "100" "0"   "120"
$x2
[1] "0" "0" "0"
$x3
[1] "110" "0"   "0" 

I want to replace the 0-only string entries to 000. That means, my data should look like,

> str(testdata)
    List of 3
     $ x1: chr [1:3] "100" "000" "120"
     $ x2: chr [1:3] "000" "000" "000"
     $ x3: chr [1:3] "110" "000" "000"

Following this, I can write this,

testdata2 <- data.frame(lapply(testdata, function(x) {gsub("0", "000", x)}))

Or this,

testdata %>% mutate_all(funs(str_replace_all(., "0", "000")))

In both cases, it replaces ALL 0s with 000. And the resulting data looks like this,

> testdata
       x1  x2    x3
1 1000000 000 11000
2     000 000   000
3   12000 000   000

which is not what I am looking for. Any idea how to solve this problem?

like image 590
small_lebowski Avatar asked Jan 01 '23 10:01

small_lebowski


1 Answers

You can also use sprintf, i.e.

lapply(testdata, function(i)sprintf('%03d', as.numeric(i)))
#$`x1`
#[1] "100" "000" "120"

#$x2
#[1] "000" "000" "000"

#$x3
#[1] "110" "000" "000"
like image 176
Sotos Avatar answered Jan 14 '23 04:01

Sotos