Find max/mean/min of the a subset in R

左心房为你撑大大i 提交于 2019-12-11 17:33:02

问题


A simple question but I can't seem to figure it out. I want to find the maximum value in a subset of my data frame.

To my knowledge, it would look something like:

max(data.frame$vector1)[data.frame$vector2=="1",]  

but I get the error message of incorrect number of dimensions.

This text is then supposed to be used as a logical test in another subscript


回答1:


Your indexing command is wrong. Try

 max( dataframe[ , "vector1" ] )                              ## all of vector1

for all, and

 max( dataframe[ dataframe[,"vector2"]==1 , "vector1" ] )     ## subset

which you can write in long form as

 sdfind <- dataframe[,"vector2"]==1
 max( dataframe[sdfind, "vector1"])

You can also use the subset() function, or create a temporary variable, or even apply the summary() function at all values of vector2, or ..



来源:https://stackoverflow.com/questions/12350783/find-max-mean-min-of-the-a-subset-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!