Reputation: 145
The problem is that there are times that my data has no numbers greater than or equal to 5 and less than 80 or there are times when I only have one observation so I can't perform a t.test. How do I use an if statement inside my loop so that if it doesn't get any value between 5 to 80 or only has one observation, it just uses NA
and doesn't perform the t.test?
Upvotes: 0
Views: 73
Reputation: 992
DWin is correct. Types matter in R and length doesn't always work the way you expect with dataframes. You should be able to use nrow
or NROW
to get the "length". If all else fails, you can use try
but that always makes me feel dirty.
To know for sure, though, please provide an example as requested by Matthew.
Upvotes: 2
Reputation: 5566
You can put a "try" block like this inside the lapply. Initialize the result to NA, and it will only assign the t.test result if it doesn't run into any errors.
data2 <- lapply(1:length(m), function(i) {
res = NA
try({
#you can put your other constraints here
if(length(m[[i]][[5]]) > 1) {
res = t.test(data1$Value,m[[i]][[5]])
}
}, silent=T)
res
})
Upvotes: 1