Here is my code and the error/warning message
library(dplyr)
df_sw = as.data.frame(starwars)
df_sw = df_sw[1:11]
final = ""
print(mean(df_sw[,1],trim = 0, na.rm = TRUE))
for(i in 1:ncol(df_sw)){
print("hello")
cat(colnames(df_sw)[i], ": ",ifelse(is.numeric(df_sw[1,i]),cat("numeric the mean is: ",mean(df_sw[,i],trim = 0, na.rm = TRUE)),"character: "))
for(b in 1:4){
cat(df_sw[b,i], " ")
}
}
[1] "hello" name : character: Luke Skywalker C-3PO R2-D2 Darth Vader [1] "hello" numeric the mean is: 174.358. Error in ans[ypos] <- rep(yes, length.out = len)[ypos] : replacement has length zero In addition: Warning message: In rep(yes, length.out = len) : 'x' is NULL so the result will be NULL
I am very new to R. For some reason, my mean() function only returns part of the mean before throwing an error. I tried writing the
mean(df_sw[,I], na.rm = TRUE)
outside the for loop, and it returned both NA and the error/warning.
Any help would be greatly appreciated