Home > Mobile >  Error in FUN(left, right) : non-numeric argument to binary operator in R
Error in FUN(left, right) : non-numeric argument to binary operator in R

Time:10-21

I would like to get PV, but when I run the code it gives the following error appears: Error in FUN(left, right) : non-numeric argument to binary operator when I do cbing. Can you help me?

 library(dplyr)
 library(tidyverse)
 library(lubridate)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-07-01","2021-07-02","2021-07-02"),
       Code = c("FDE","ABC","ABC","FDE"),
       Week= c("Wednesday","Wednesday","Friday","Friday"),
       DR1 = c(4,1,4,1),
       DR01 = c(4,1,4,2), DR02= c(4,2,6,2),DR03= c(9,5,4,2),
       DR04 = c(5,4,3,3),DR05 = c(5,4,5,2),
       DR06 = c(2,4,3,3),DR07 = c(2,5,4,2),
       DR08 = c(0,0,0,4),DR09 = c(0,0,0,2),DR010 = c(0,0,0,2),DR011 = c(4,0,0,2), 
       DR012 = c(0,0,0,""), DR013 = c(0,0,1,""), DR014 = c(0,0,0,"")),
  class = "data.frame", row.names = c(NA, -4L))

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

x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))

PV<-select(x, date2,Week, Code, DR1, ends_with("PV"))

CodePudding user response:

In x DR012 to DR14 columns as character class. So you can yous type.convert(as.is=TRUE) to solve your problem:

library(dplyr)
library(tidyverse)
library(lubridate)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-07-01","2021-07-02","2021-07-02"),
       Code = c("FDE","ABC","ABC","FDE"),
       Week= c("Wednesday","Wednesday","Friday","Friday"),
       DR1 = c(4,1,4,1),
       DR01 = c(4,1,4,2), DR02= c(4,2,6,2),DR03= c(9,5,4,2),
       DR04 = c(5,4,3,3),DR05 = c(5,4,5,2),
       DR06 = c(2,4,3,3),DR07 = c(2,5,4,2),
       DR08 = c(0,0,0,4),DR09 = c(0,0,0,2),DR010 = c(0,0,0,2),DR011 = c(4,0,0,2), 
       DR012 = c(0,0,0,""), DR013 = c(0,0,1,""), DR014 = c(0,0,0,"")),
  class = "data.frame", row.names = c(NA, -4L))

x<-df1 %>% select(starts_with("DR0")) %>% type.convert(as.is=TRUE)
x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))

PV<-select(x, date2,Week, Code, DR1, ends_with("PV"))
PV

output:

       date2      Week Code DR1 DR01_PV DR02_PV DR03_PV DR04_PV DR05_PV DR06_PV DR07_PV DR08_PV DR09_PV
1 2021-06-30 Wednesday  FDE   4       0       0      -5      -1      -1       2       2       4       4
2 2021-07-01 Wednesday  ABC   1       0      -1      -4      -3      -3      -3      -4       1       1
3 2021-07-02    Friday  ABC   4       0      -2       0       1      -1       1       0       4       4
4 2021-07-02    Friday  FDE   1      -1      -1      -1      -2      -1      -2      -1      -3      -1
  DR010_PV DR011_PV DR012_PV DR013_PV DR014_PV
1        4        0        4        4        4
2        1        1        1        1        1
3        4        4        4        3        4
4       -1       -1       NA       NA       NA
  •  Tags:  
  • r
  • Related