I have a problem in producing a pdf document by using RStudio
and Sweave
. After running the code, there is no error message and no warning message. Nevertheless, when I type in in the console warnings()
I get a list of things of which here is an excerpt, the rest of the warnings look exactly the same:
`Warning messages:
1: In normality_test(df, i, j) : NAs introduced by coercion
2: In normality_test(df, i, j) : NAs introduced by coercion
3: In normality_test(df, i, j) : NAs introduced by coercion
4: In normality_test(df, i, j) : NAs introduced by coercion
5: In if (shapiro.test(df[, i]) > 0.05 & shapiro.test(df[, ... :
the condition has length > 1 and only the first element will be used
6: In normality_test(df, i, j) : NAs introduced by coercion`
The missing values (NAs) were accordingly discarded in the code before I had to get aware of the warnings. In order to solve the issue I used the command df[is.na(df)] <- 0
. It did not change anything. The same warnings persist. To the contrary, I observe that figures are generated just like one would expect. All the code about which there is some warning shown above,works perfectly when run in RStudio
but not linked via sweave
. It seems contradictory and odd. I desperately tried for hours without success.
Do you have any idea as to how to solve the issue ?
I am using the penguins
data set. Here is the code used:
df <- read.csv("penguins.csv")
str(df)
#We transform the character variables type into factor ones
i <- sapply(df, is.character)
df[,i] <- lapply(df[,i], as.factor)
df[,8] <- as.factor(df[,8])
str(df)
normality_test <- function(df,i,j) {
df <- df[!is.na(df[,i])&!is.na(df[,j]),]
plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
if (shapiro.test(df[,i]) > 0.05 & shapiro.test(df[,j]) > 0.05){
res1 <- cor.test(df[,i],df[,j],
method = "pearson")
text(.5, .5, paste("p.value:", round(res1$p.value,2), "\n r:", round(res1$estimate,2)))
}
else {
res2 <- cor.test(df[,i],df[,j],
method = "spearman")
text(.5,.5, paste("p.value:", round(res2$p.value,2), "rho:", round(res2$estimate,2)))
}
}
#We define the density function to include diagonal elements
hist_density <- function (df, i) {
tmp <- na.omit(df[,i])
hist(tmp, col = "light blue",
probability = TRUE, main=NULL)
lines(density(tmp), col = "red", lwd = 1.5)
}
new_pairs <- function(df, x){
par(mar=c(1,1,1,1))
n_col<-sum(sapply(df, is.numeric))
par(mfrow=c(n_col,n_col))
n<-ncol(df)
for (i in 1:n){
for (j in 1:n){
if ((class(df[,i])!="factor" ) & (class(df[,j])!="factor") & i<j) {
plot(df[,i], df[,j], col = df[,x])
}
else if ((class(df[,i])!="factor") & (class(df[,j])!="factor") & i==j) {
hist_density(df, i)
}
else if ((class(df[,i])!="factor" ) & (class(df[,j])!="factor") & i>j){
normality_test(df,i,j)
}
else {NA}
}
}
}
new_pairs(df, 2)