Home > front end >  Round numeric values in comma-separated string column
Round numeric values in comma-separated string column

Time:12-14

I want to round values in a comma-separated string column:

df <- structure(list(id = 1:8,
                     value = c("0.0081007", NA, NA,"0.00699123", 
                               "0.175555, 0.106897, 0.0289, 0.255005", 
                               NA, NA, "0.0047777, 0.8970001")), 
                class = c("tbl_df", "tbl", "data.frame"), 
                row.names = c(NA, -8L))

I can do it as shown below but doubt that this is the most concise/most efficient way:

library(dplyr)
df %>%
  mutate(value = lapply(str_extract_all(value, "[\\d.] "), function(x) round(as.numeric(x), 3))) %>%
  unnest(value) %>%
  group_by(id) %>%
  summarise(
    value = toString(value))
# A tibble: 8 × 2
     id value                     
  <int> <chr>                     
1     1 0.008                     
2     2 NA                        
3     3 NA                        
4     4 0.007                     
5     5 0.176, 0.107, 0.029, 0.255
6     6 NA                        
7     7 NA                        
8     8 0.005, 0.897 

Is there a shorter/more concise dplyr way?

CodePudding user response:

You can make this a bit more concise with separate_rows().

library(dplyr)
library(tidyr)

df %>% 
  separate_rows(value, sep = ",", convert = TRUE) %>% 
  group_by(id) %>%
  summarise(value = toString(round(value, 3)))

CodePudding user response:

Look, Mum, no tidyverse!

df$value <- sapply(
    sprintf('round(c(%s), 3)', df$value), 
    \(x) { toString(eval(str2expression(x)))  }
)

df

# # A tibble: 8 x 2
#      id value                     
#   <int> <chr>                     
# 1     1 0.008                     
# 2     2 NA                        
# 3     3 NA                        
# 4     4 0.007                     
# 5     5 0.176, 0.107, 0.029, 0.255
# 6     6 NA                        
# 7     7 NA                        
# 8     8 0.005, 0.897   
  • Related