问题
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