Question

I have a matrix of data, and I want to return the summary/mean of specific columns, after a boolean command has been issued.

I tried:

by(data$total > 500, data$operation1 == 1 & data$operation2 == 1, summary)

However it just returns, the number of results that are TRUE or FALSE. Not the actual mean/summary of the totals > 500.

I then tried:

summary(subset(data, data$total > 500 & data$operation1 == 1 & data$operation2 == 1))

Which did work, however it returned all the subset of all the columns in my data, and not just the totals > 500, which is what I'm looking for.

I have a feeling the correct answer is a mix of the subset() and by() commands, but I'm coming up with a blank.

Thanks for your input.

Was it helpful?

Solution

This is a possibility:

# build small dataset
factor1 <- factor(rep(1:2,each=25))
factor2 <- factor(rep(3:4,each=25))  
data<-rnorm(50,500,50) 
alt.data<-rnorm(50,500,50)
frame <- data.frame(factor1,factor2,data,alt.data)

# subset the dataframe
subset(frame, data>500 & factor1==1 & factor2==3)

# summarize the one variable
summary(subset(frame, data>500 & factor1==1 & factor2==3)[,3]) 

# or if you want multiple columns
summary(subset(frame, data>500 & factor1==1 & factor2==3)[,3:4]) 

If I am interpreting your question correctly.

OTHER TIPS

Try this

data <- data.frame(total = sample(seq(490,510), 10),
                   operation1 =  sample(seq(1,2), 10, replace = T),
                   operation2 = sample(seq(1,2), 10, replace = T),
                   ColumnToSum1 = rnorm(10, 2, 6),
                   ColumnToSum2 = rnorm(10, 2, 6)) # Your data


summary(data[data$total > 500 & data$operation1 == 1 & data$operation2 == 1, c("ColumnToSum1", "ColumnToSum2")])
colMeans(data[data$total > 500 & data$operation1 == 1 & data$operation2 == 1, c("ColumnToSum1", "ColumnToSum2")], na.rm = T)

Example results:

  ColumnToSum1       ColumnToSum2   
 Min.   :-0.99907   Min.   : 6.973  
 1st Qu.:-0.08076   1st Qu.: 9.001  
 Median : 0.83755   Median :11.028  
 Mean   : 0.83755   Mean   :11.028  
 3rd Qu.: 1.75586   3rd Qu.:13.055  
 Max.   : 2.67416   Max.   :15.082  

ColumnToSum1 ColumnToSum2 
   0.8375483   11.0277917 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top