[R] result numeric(0) when using variable1[which(variable2="max(variable2)"]

Duncan Murdoch murdoch.duncan at gmail.com
Tue Jan 17 15:22:59 CET 2012


On 17/01/2012 5:35 AM, Nerak wrote:
> Dear all,
> I have a question about the knowing for which row I have the max value of
> one of my variables.
> I calculated the Rsquared for different columns and made a list to gather
> them. I unlisted this list to create a vector with this values. I want to
> know for which column I have the max value of Rsquared.
> The columns were always named in the same way. They always start with
> results4$depth_ following by the number. The numbers are constructed as:
> seq(1,10,0.1). But if the R squared values are now in 1 column, I don’t know
> for which column they are calculated. So I made a new data frame with both
> columns:
> R2<- unlist(LIST)
> Cvalue<- c(seq(1,10,0.1))
> results5<- data.frame(Cvalue,R2)
>
> # I know I can calculate the max value of Rsquared by this way:
>
> max(results5$R2)
>
> # now I want to know to which Cvalue this belongs. I would write it like
> this:
> results5$Cvalue[which(results5$R2 == "max(results5$R2)")]

Don't use quotes on the expression.  None of your R2 values are the string

"max(results5$R2)"

which is why you're getting numeric(0).

You can also make it simpler by using the which.max() function.

Duncan Murdoch

> # But I always get the solution:
> numeric(0)
> # I don’t know if these Rsquared values are in a kind of format that this
> doesn’t work? (I used before for similar things, and I experienced that for
> example it cannot works if R recognizes the values as a date).  Maybe
> because it’s with a lot of decimals? (eg 2.907530e-01) I know that
> max(results5$R2) is in this example 0.6081547 and I can see that that
> belongs to the Cvalue == 1.8. It works in the opposite way.
> results5$R2[which(results5$Cvalue == "1.8")]
> # But neither
> results5$Cvalue[which(results5$R2 == "0.6081547")]
> # nor
> results5$Cvalue[which(results5$R2 == "max(results5$R2)")]
> # works…
>
> I hope someone can help me with this problem
> Kind regards
> Nerak
>
> --
> View this message in context: http://r.789695.n4.nabble.com/result-numeric-0-when-using-variable1-which-variable2-max-variable2-tp4302887p4302887.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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