Reputation: 773
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?
Upvotes: 4
Views: 238
Reputation: 51622
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"
Upvotes: 3
Reputation: 11957
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)
If it's possible to keep your data in data.frame format, the following would work:
testdata <- as.data.frame(testdata, stringsAsFactors = F)
testdata[testdata == '0'] <- '000'
x1 x2 x3
1 100 000 110
2 000 000 000
3 120 000 000
Upvotes: 2
Reputation: 76683
In base R, there is sub
with the appropriate regex.
lapply(testdata, function(x) sub("^0$", "000", x))
#$x1
#[1] "100" "000" "120"
#
#$x2
#[1] "000" "000" "000"
#
#$x3
#[1] "110" "000" "000"
Explanation: "^"
marks the beginning and "$"
marks the end of the string. So the pattern "^0$"
is comprised of the character "0"
and only of that one character.
Upvotes: 1
Reputation: 887981
We can use ifelse
with strrep
in base R
lapply(testdata, function(x) ifelse(x == 0, strrep(x, 3), x))
#$x1
#[1] "100" "000" "120"
#$x2
#[1] "000" "000" "000"
#$x3
#[1] "110" "000" "000"
In the OP's post, it is replacing "0"s with gsub
or str_replace_all
which matches all the "0" digit instead of just checking whether the value is 0 or not
Upvotes: 1
Reputation: 5456
Or:
library(tidyverse)
testdata %>%
map_df(~if_else(.x == "0", "000", .x))
# A tibble: 3 x 3
#x1 x2 x3
#<chr> <chr> <chr>
# 1 100 000 110
#2 000 000 000
#3 120 000 000
Upvotes: 2