We are celebrating the 10-year anniversary of the Alteryx Community! Learn more and join in on the fun here.
Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Subsetting Data in R tool gives warning of incompatible methods

Ashish
8 - Asteroid

Hello,

 

I am trying to subset my data based on a date, in the R tool and I get this warning

Warning: R (34): Incompatible methods ("Ops.factor", "Ops.POSIXt") for ">" 

 which  leads to undesirable results.

 

Pasted below is the code I am using (I have tried couple different lines of code but all of them gives the same warning).

All the options I tried (commented in the code below works fine in Rstudio)

 

# Read the data stream into R
Input <- read.Alteryx("#1", mode="data.frame")


season_matrix <- model.matrix(Input$Change ~Input$Season)[,-1]
Input <- cbind(Input,season_matrix)
#Input$Date <- as.Date(Input$Date)
#the.data_test <- Input[ Input$Date >= as.POSIXct("2015-12-31"),]
#the.data_train <- Input[ Input$Date <= as.POSIXct("2015-12-31"),]

the.data_test <- Input[ which(Input$Date > as.POSIXct("2015-12-31")),]
the.data_train <- Input[ which(Input$Date <= as.POSIXct("2015-12-31")),]

#the.data_test <- Input[ format(Input$Date,"%Y-%m-%d") >= "2015-12-31",]
#the.data_train <- Input[ format(Input$Date,"%Y-%m-%d") <= "2015-12-31",]

#the.data_test <- subset(Input , as.Date(Date) > as.POSIXct("2015-12-31"))
#the.data_train <- subset(Input , as.Date(Date) <= as.POSIXct("2015-12-31"))

Can anyone please give some idea on how to fix this.

 

Thanks,

Ashish

1 REPLY 1
Ashish
8 - Asteroid

ok, I figured out the fix for the problem. Although I do not fully understand why it was not working earlier and why its working now, but somehow it worked.

 

I used as.Date function on both sides of operator to coerce the type.

 

 

require(glmnet)
require(stats)
# Read the data stream into R
Input <- read.Alteryx("#1", mode="data.frame")
season_matrix <- model.matrix(Input$Change ~Input$Season)[,-1]
Input <- cbind(Input,season_matrix)

#Input$Date <- as.Date(Input$Date)
#the.data_test <- Input[ Input$Date >= as.POSIXct("2015-12-31"),]
#the.data_train <- Input[ Input$Date <= as.POSIXct("2015-12-31"),]

the.data_test <- Input[ as.Date(Input$Date) >= as.Date("2015-12-31"),]
the.data_train <- Input[ as.Date(Input$Date) <= as.Date("2015-12-31"),]

The "Date" variable in the dataset was already of type date so I did not suspect it earlier.

 

 

Also when I tried formula tool create a new Date variable date format that did not work either.

 

I am relieved that I've found a fix but I lack the understanding what works and why.

 

Thanks,

Labels
Top Solution Authors