0

So i have this csv data which have a couple of String variable, I wanted to change it to integer how can I do that? the data is >200000 i want to change it to 200000

  • 1
    try `as.numeric("200000")` – Rajith Thennakoon Mar 23 '20 at 06:38
  • 1
    Hi! See: https://stackoverflow.com/questions/11810605/replace-contents-of-factor-column-in-r-dataframe – Christian Johansson Mar 23 '20 at 06:42
  • 1
    Please add data using `dput` and show the expected output for the same. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Mar 23 '20 at 07:52

2 Answers2

1

Remove punctuation, then convert to numeric:

x = c(1, 2, ">200")
as.numeric(gsub("[[:punct:]]", "", x))
# [1] 1 2 200

Or, for a data in a column:

data$x = as.numeric(gsub("[[:punct:]]", "", data$x))
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
-1

Since i have noting to go on, I will give a general solution.

Data:

col <- sample(1:100, 5, replace=FALSE)
col <- c(col, ">200000")
df <- as.data.frame(col, stringsAsFactors = FALSE)

Solution 1:

df$col[df$col == ">200000"] <- "200000"
df$col <- as.integer(df$col)

Solution 2:

df$col <- replace(df$col, df$col == ">200000", "200000")
df$col <- as.integer(df$col)
E_net4
  • 27,810
  • 13
  • 101
  • 139