1

My data frame looks like this

value <- c(0,0.1,0.2,0.4,0,"0.05,",0.05,0.5,0.20,0.40,0.50,0.60)
time <- c(1,1,"1,",1,2,2,2,2,3,3,3,3)
ID <- c("1,","2,","3,",4,1,2,3,4,1,2,3,4)

test <- data.frame(value, time, ID)
test

   value time ID
1      0    1 1,
2    0.1    1 2,
3    0.2   1, 3,
4    0.4    1  4
5      0    2  1
6  0.05,    2  2
7   0.05    2  3
8    0.5    2  4
9    0.2    3  1
10   0.4    3  2
11   0.5    3  3
12   0.6    3  4

I want to replace the "," from all columns with "" but I am still getting an error Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "character" I would like my data to look like this

  value time ID
1   0.00    1  1
2   0.10    1  2
3   0.20    1  3
4   0.40    1  4
5   0.00    2  1
6   0.05    2  2
7   0.05    2  3
8   0.50    2  4
9   0.20    3  1
10  0.40    3  2
11  0.50    3  3
12  0.60    3  4

EDIT

test %>% 
mutate_all(~gsub(",","",.))

3 Answers 3

4

The easiest in this case might be to use parse_number from the readr package, e.g. :

apply(test, 2, readr::parse_number)

or in dplyr lingo:

test %>% mutate_all(readr::parse_number)
Sign up to request clarification or add additional context in comments.

Comments

3

A simple base Rsolution:

test <- sapply(test, function(x) as.numeric(sub(",", "", x)))
test
      value time ID
 [1,]  0.00    1  1
 [2,]  0.10    1  2
 [3,]  0.20    1  3
 [4,]  0.40    1  4
 [5,]  0.00    2  1
 [6,]  0.05    2  2
 [7,]  0.05    2  3
 [8,]  0.50    2  4
 [9,]  0.20    3  1
[10,]  0.40    3  2
[11,]  0.50    3  3
[12,]  0.60    3  4

Comments

2
test %>%
  mutate_at(vars(value, time, ID), ~ gsub(".*?(-?[0-9]+\\.?[0-9]*).*", "\\1", .))
#    value time ID
# 1      0    1  1
# 2    0.1    1  2
# 3    0.2    1  3
# 4    0.4    1  4
# 5      0    2  1
# 6   0.05    2  2
# 7   0.05    2  3
# 8    0.5    2  4
# 9    0.2    3  1
# 10   0.4    3  2
# 11   0.5    3  3
# 12   0.6    3  4

The more we get into the "let's try to parse what could be a number", it can get crazy, including scientific notation. For that, readr::parse_number already suggested is likely a better candidate if you can accept one more package dependency.

However ... seeing this suggests that either the method of import has some mistakes in it, or however the data is formed has mistakes in it. While this patch works on those kinds of mistakes, it is far better to fix whichever error is causing this.

3 Comments

value still has a comma though
Thank you for your time and your detailed post r2evans, I highly appreciate
btw, one premise of my answer was to keep the data as a string, even if it looks like a number. Your expected output does suggest converting to numeric and integer, but you didn't state it. No problem either way, as all offered methods have pros/cons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.