Home > Software engineering >  Converting character to integer when it is possible
Converting character to integer when it is possible

Time:12-16

I have the following data frame:

y  <- c("11 - 14", "13 - 17", "13 - 19")
x1 <- c(10, 11, 8)
x2 <- c(31, 30, 30)

df <- data.frame(y, x1, x2)

How can I convert the character to a unique integer such as mean?

For example, "11 - 14" becomes 12.5.

CodePudding user response:

Split on " - " and then just convert each to numeric and take the mean.

y_split <- strsplit(df$y, " - ")
df$y <- sapply(y_split, function(x) mean(as.numeric(x)))
df
#>      y x1 x2
#> 1 12.5 10 31
#> 2 15.0 11 30
#> 3 16.0  8 30

CodePudding user response:

Same method as the other answer but using tidyverse packages:

library(purrr)
library(stringr)
library(dplyr)
df %>%
  mutate(
    y_nums = str_extract_all(y, pattern = "[[:digit:]] "),
    result = map(y_nums, .f = ~mean(as.numeric(.)))
  )
#         y x1 x2 y_nums result
# 1 11 - 14 10 31 11, 14   12.5
# 2 13 - 17 11 30 13, 17     15
# 3 13 - 19  8 30 13, 19     16

CodePudding user response:

We may do this with rowMeans and read.table - read the column 'y' with read.table to create two columns, and get the rowwise mean with rowMeans in base R

df$result <- rowMeans(read.table(text=df$y, sep="-", strip.white = TRUE))

-output

> df
        y x1 x2 result
1 11 - 14 10 31   12.5
2 13 - 17 11 30   15.0
3 13 - 19  8 30   16.0

CodePudding user response:

Another option with base R:

df$y <- do.call(rbind, strsplit(df$y, "-")) |>
  type.convert(as.is = TRUE) |>
  rowMeans()

which gives:

> df
     y x1 x2
1 12.5 10 31
2 15.0 11 30
3 16.0  8 30

Even another option (not highly recommended though):

df$y <- sapply(sub("-", " ", df$y), \(x) eval(parse(text = x))) / 2
  • Related