Convert chararcter vector to numeric vector

Hi,

I have a dfv such as

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
a <- c("4", "3,4", "7")
b <- c("3,5", "3", "7,4")

df <- tibble(a,b)

and I would like to get mean value for each value. In this case, it would be:

a <- c(4, 3.5, 7)
b <- c(4, 3, 5.5)

I will appreciate your help.

Many thanks,

Jakub

I don't understand this part of your post :

so, that aside, if you want the character strings interpreted as numeric values, then do this.

library(tidyverse)

a <- c("4", "3,4", "7")
b <- c("3,5", "3", "7,4")

df <- tibble(a,b)

(df2 <- mutate(df,
               across(everything(),
                      ~parse_number(str_replace_all(.x,",",".")))))

if you have further requirements for what to do with these numbers, please ask again with more clarity around that please.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.