[R] argument to 'which' is not logical
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Wed Mar 22 15:19:34 CET 2017
The `c` function is extremely common. You CAN redefine this object as a numeric variable if you want but I strongly recommend against it.
I don't recognize the function you are using to create cvfits, but it looks like the coef method for that object is not returning a numeric vector, so your comparison is with some other kind of object.
Notice how ugly your code looks below... only you can fix this by setting your email program to send plain text to the mailing list.
--
Sent from my phone. Please excuse my brevity.
On March 22, 2017 5:15:04 AM PDT, Allan Tanaka <allantanaka11 at yahoo.com> wrote:
>I'm trying to run the code: inds<-which(c != 0 ), but it gave me
>error: Error in base::which(x, arr.ind, useNames, ...) : argument to
>'which' is not logical
>Here is code:
>alphas <- seq(0, 1, by=.002)mses <- numeric(501)mins <-
>numeric(501)maxes <- numeric(501)for(i in 1:501){ cvfits <-
>cv.glmnet(Train2, Train$Item_Outlet_Sales, alpha=alphas[i], nfolds=32)
>loc <- which(cvfits$lambda==cvfits$lambda.min) maxes[i] <-
>cvfits$lambda %>% max mins[i] <- cvfits$lambda %>% min mses[i] <-
>cvfits$cvm[loc]}`%ni%`<-Negate(`%in%`)c<-coef(cvfits,s='lambda.1se',exact=TRUE)inds<-which(c
>!= 0 )
>Here is the c content looks like: > c33 x 1 sparse Matrix of class
>"dgCMatrix" 1(Intercept)
> 7.476895931Item_Fat_Content.Low.Fat .
> Item_Fat_Content.Regular . Item_Type.Breads
> . Item_Type.Breakfast .
> Item_Type.Canned 0.003430669Item_Type.Dairy
> -0.022579673Item_Type.Frozen.Foods
> -0.008216547Item_Type.Fruits.and.Vegetables .
> Item_Type.Hard.Drinks .
> Item_Type.Health.and.Hygiene . Item_Type.Household
> . Item_Type.Meat .
> Item_Type.Others . Item_Type.Seafood
> . Item_Type.Snack.Foods .
> Item_Type.Soft.Drinks . Item_Type.Starchy.Foods
> . Outlet_Establishment_Year.1987
> -0.345927916Outlet_Establishment_Year.1997
>1.692678186Outlet_Establishment_Year.1998
> -2.259508290Outlet_Establishment_Year.1999 .
> Outlet_Establishment_Year.2002
> -0.032971913Outlet_Establishment_Year.2004
>1.756230495Outlet_Establishment_Year.2007 .
> Outlet_Establishment_Year.2009 -0.549210057Outlet_Size.Medium
> 0.056897825Outlet_Size.Small
>-1.706006538Outlet_Location_Type.Tier.3
> 0.373456218Item_Identifier_CombinedFD . Item_MRP
> 0.505435352Item_Weight .
> Item_Visibility_MeanRatio -0.007274202
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list