Jennifer Collins
Jennifer Collins

Reputation: 253

R: How to find the minimum value in row that has both numeric and non-numeric items?

I have a matrix - columns 1-371 are numeric, and columns 372-379 are non-numeric (ie. stores the age, gender information). I want to find the minimum value of each row of the numeric items (for each row, look over the 371 values).

I'm trying to make a count vector, so the code is:

count_a <- 0
for (i in 1:nrow(data)) {
    if (min(data[i,][which(data$Age < age & data$Gender == gender)]) <= threshold) {
        count_a <- count_a+1
    }
}

However I keep getting this error: Error in FUN(X[[1L]], ...) : only defined on a data frame with all numeric variables

What should I do? Thanks!

Upvotes: 1

Views: 4518

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 109924

Using the CO2 data set try something like this:

NUM <-function(dataframe)dataframe[,sapply(dataframe,is.numeric)]
apply(NUM(CO2), 1, min)

Upvotes: 2

Related Questions