R for loop not working

后端 未结 3 1470
予麋鹿
予麋鹿 2020-12-22 08:41

I\'m trying to use R to find the max value of each day for 1 to n days. My issue is there are multiple values in each day. Heres my code. After I run it incorrect number of

3条回答
  •  -上瘾入骨i
    2020-12-22 08:58

    This is a base function approach:

    > do.call( rbind, lapply(split(dfrm, dfrm$Day), 
                             function (df) df[ which.max(df$Value), ] ) )
                  Day     Time Value
    20130310 20130310 09:30:00     5
    20130311 20130311 09:30:00    12
    

    To explain what's happening it's good to learn to read R functions from the inside out (since they are often built around each other.) You wanted lines from a dataframe, so you would either need to build a numeric or logical vector that spanned the number of rows, .... or you can take the route I did and break the problem up by Day. That's what split does with dataframes. Then within each dataframe I applied a function, which.max to just a single day's subset of the data. Since I only got the results back from lapply as a list of dataframes, I needed to squash them back together, and the typical method for doing so is do.call(rbind, ...).

    If I took the other route of making a vector for selection that applied to the whole dataframe I would use ave:

    > dfrm[ with(dfrm, ave(Value, Day, FUN=function(v) v==max(v) ) ) , ]
             Day     Time Value
    1   20130310 09:30:00     5
    1.1 20130310 09:30:00     5
    

    Huh? That's not right... What's the problem?

    with(dfrm, ave(Value, Day, FUN=function(v) v==max(v) ) )
    [1] 1 0 0 0 1 0 0 0
    

    So despite asking for a logical vector with the "==" function, I got conversion to a numeric vector, something I still don't understand. But converting to logical outside that result I succeed again:

    > dfrm[ as.logical( with(dfrm, ave(Value, Day, 
                                       FUN=function(v) v==max(v) ) ) ), ]
           Day     Time Value
    1 20130310 09:30:00     5
    5 20130311 09:30:00    12
    

    Also note that the ave function (unlike tapply or aggregate) requires that you offer the function as a named argument with FUN=function(.). That is a common error I make. If you see the "error message unique() applies only to vectors", it seems out of the blue, but means that ave tried to group an argument that it expected to be discrete and you gave it a function.

提交回复
热议问题