I have a data set such as the following (this is only a subset in which the forloop might produce invalid results).
tableData <- data.frame(Fruits = character(), Ripeness = character(), Mean = numeric()) %>%
add_row(Fruits = "Apple", Ripeness = "yes", Mean = 5) %>%
add_row(Fruits = "Apple", Ripeness = "no", Mean = 6) %>%
add_row(Fruits = "Apple", Ripeness = "yes", Mean = 2) %>%
add_row(Fruits = "Banana", Ripeness = "yes", Mean = 1) %>%
add_row(Fruits = "Banana", Ripeness = "yes", Mean = 7) %>%
add_row(Fruits = "Orange", Ripeness = "no", Mean = 8)
The following for loop produces a t-test of the Mean for each category of fruits by their ripeness.
finalOut <- data.frame(Fruits = character(), Mean = numeric())
fruitLoop <- function(x) {
fruit <- unique(x$Fruits)
for(i in 1:length(fruit)){
df<-filter(x, Fruits==fruit[i])
fruit[i]
ripe <- unique(df$Ripeness)
if(length(ripe)<2) {
next
}
tryResult <- tryCatch(
{
t.test(Mean ~ Ripeness, data = df)$p.value
},
error=function(cond){
}
)
finalOut[i,] <- c(fruit[i],tryResult)
}
}
I want the results of the t-test printed into the finalOut, however it doesn't seem to print into it. How can I achieve this? Again, the data is only a subset, and might be insufficient for the forloop to run.