0

I have a column Apps in dataframe dframe

that looks like this:

    Apps
1    31
2    12
3    10
4    33
5    -

I need the column to be type int instead of String so I need to convert the 5th row to a 0.

    Apps
1    31
2    12
3    10
4    33
5    0
Paul R
  • 79
  • 1
  • 7
  • 4
    How did you read this data into R? It probably would have been a good idea to use `read.table(..., na.strings="-")` to read it in as missing from the beginning. – MrFlick Dec 14 '18 at 21:23
  • 2
    `dframe$Apps[ dframe$Apps == "-" ] – r2evans Dec 14 '18 at 21:25

3 Answers3

3
dframe$Apps[dframe$Apps == "-"] <- "0"
dframe$Apps <- as.integer(dframe$Apps)
Max Ghenis
  • 12,769
  • 13
  • 73
  • 119
0

You can do it with ifelse and the tidyverse approach:

require(tidyverse)

df %>% 
  mutate(Apps = ifelse(Apps == "-", 0, Apps))

  Apps
1    4
2    3
3    2
4    5
5    0

Dataset:

df <- read.table(text = "    Apps
1    31
2    12
3    10
4    33
5    -", header = TRUE)
DJV
  • 4,209
  • 3
  • 15
  • 33
0
dframe$Apps <- as.integer(gsub("-", "0", dframe$Apps, fixed = TRUE))

will give you an integer column as I suspect you want.