Robert
Robert

Reputation: 151

Removing specific integers from columns in a dataframe using R

Here is what my current dataframe looks like:

Name <- c("John", "Paul", "Ringo", "George")
Loc1 <- c("001001", "001002", "001002", "002002")
Loc2 <- c("001002", "002002", "001002", "001001")
Loc3 <- c("002002", "001002", "001002", "001001")
dat <- data.frame(Name, Loc1, Loc2, Loc3)
dat

And this is my desired result:

Name <- c("John", "Paul", "Ringo", "George")
Loc1 <- c("11", "12", "12", "22")
Loc2 <- c("12", "22", "12", "11")
Loc3 <- c("22", "12", "12", "11")
result <- data.frame(Name, Loc1, Loc2, Loc3)
result

Note that the number remain the same, but I am looking for a way fro remove the "0" characters from multiple columns and leave behind other integers. I have tried using various incarnations of the strstring() and substr() functions for this, but seem to keep getting stuck because "0" is not recognized as a character. It seems a fairly simple problem but I need some advice. Thanks in advance.

Upvotes: 1

Views: 52

Answers (3)

akrun
akrun

Reputation: 887108

Here is an option with tidyverse

library(tidyverse)
dat %>%
     mutate_at(vars(matches("Loc")), funs(str_replace_all(., "0", "")))
#    Name Loc1 Loc2 Loc3
#1   John   11   12   22
#2   Paul   12   22   12
#3  Ringo   12   12   12
#4 George   22   11   11

Upvotes: 1

KoenV
KoenV

Reputation: 4283

in order for this to work you need both gsub and a member of the apply family, like this:

Name <- c("John", "Paul", "Ringo", "George")
Loc1 <- c("001001", "001002", "001002", "002002")
Loc2 <- c("001002", "002002", "001002", "001001")
Loc3 <- c("002002", "001002", "001002", "001001")
dat <- data.frame(Name, Loc1, Loc2, Loc3)

data.frame(sapply(dat, function(x) {gsub("0", "", x)}))

# Name Loc1 Loc2 Loc3
# 1   John   11   12   22
# 2   Paul   12   22   12
# 3  Ringo   12   12   12
# 4 George   22   11   11

Upvotes: 3

Rich Scriven
Rich Scriven

Reputation: 99331

Quick and easy gsub over all columns except the first.

dat[-1] <- lapply(dat[-1], gsub, pattern = "0", replacement = "")
dat
#     Name Loc1 Loc2 Loc3
# 1   John   11   12   22
# 2   Paul   12   22   12
# 3  Ringo   12   12   12
# 4 George   22   11   11

Upvotes: 2

Related Questions