Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Solve Problems to Generate Numerical Values

I’m not able to transform them into numerical values. I did it as follows: df1<-df1 %>% mutate(across(starts_with("DR1"), as.numeric)). What am I wrong about?

df1<-structure(list(date1 = structure(c(1639612800, 1639612800, 1639612800, 
1639612800, 1639612800, 1639612800), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), date2 = structure(c(1636934400, 1636934400, 
1636934400, 1636934400, 1636934400, 1636934400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Week = c("Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday"), Category = c("ABC", "ABC", "CDE", 
"CDE", "FDG", "FDG"), DR1 = c("198.00", "401.38", "0.00", "0.00", 
 "0.00", "0.00"), DR01 = c("188.36", "293.91", "0.00", "0.00", 
 "0.00", "0.00"), DR02 = c("195.00", "282.38", "0.00", "0.00", 
"0.00", "0.00"), DR03 = c("195.00", "288.38", "0.00", "0.00", 
 "0.00", "0.00")), row.names = c(NA, -6L), class = c("tbl_df", 
 "tbl", "data.frame"))

df1<-df1 %>% mutate(across(starts_with("DR1"), as.numeric))

x<-df1 %>% select(starts_with("DR0"))

x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
Error in FUN(left, right) : non-numeric argument to binary operator

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All the DR columns here are character class. The OP converted only the ‘DR1’ columns to numeric. Thus we get the error when doing the substraction with DR0 columns. The three step process which includes creating another object ‘x’ and then cbind can be done more compactly within tidyverse itself – loop across the ‘DR’ columns and convert to numeric, then loop across the ‘DR0’ columns, subtract from ‘DR1’ and create new columns by modifying the .names ({.col} – returns the original column name, _PV adds a suffix to the column name and thus it returns new columns

library(dplyr)
df1 <- df1 %>%
    mutate(across(starts_with("DR"), as.numeric), 
          across(starts_with("DR0"), ~ DR1 - ., .names = "{.col}_PV"))

-output

df1
# A tibble: 6 × 11
  date1               date2               Week   Category   DR1  DR01  DR02  DR03 DR01_PV DR02_PV DR03_PV
  <dttm>              <dttm>              <chr>  <chr>    <dbl> <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>
1 2021-12-16 00:00:00 2021-11-15 00:00:00 Monday ABC       198   188.  195   195     9.64       3       3
2 2021-12-16 00:00:00 2021-11-15 00:00:00 Monday ABC       401.  294.  282.  288.  107.       119     113
3 2021-12-16 00:00:00 2021-11-15 00:00:00 Monday CDE         0     0     0     0     0          0       0
4 2021-12-16 00:00:00 2021-11-15 00:00:00 Monday CDE         0     0     0     0     0          0       0
5 2021-12-16 00:00:00 2021-11-15 00:00:00 Monday FDG         0     0     0     0     0          0       0
6 2021-12-16 00:00:00 2021-11-15 00:00:00 Monday FDG         0     0     0     0     0          0       0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading