I have a dataset with 74 columns, and I am trying to create a histogram for each one of these in a for loop. When run in the console, my code is fine, but when I try to knit it, R says that my data is not numeric. Anyone know why? I'm also open to other solutions to this problem of plotting multiple histograms (with the column name as an identifier on the plot). Below is a simplified representation of my problem.
library(dplyr)
data2 <- data.frame(c(1,3,5,5,2,2,1,1,1,1),
c(2,4,2,3,4,5,1,2,3,3))
colnames(data2) <- c("A", "B")
for (cols in colnames(data2)) {
data2 %>% select(cols) %>% hist()
}
Again, works fine line-by-line, but I end up receiving the following error when I try to knit it:
"Error in hist.default(.) : 'x' must be numeric Calls: ...freduce -> withVisible -> -> hist -> hist.default
Execution halted"
Interestingly, this code knits fine:
library(dplyr)
data2 <- data.frame(c(1,3,5,5,2,2,1,1,1,1),
c(2,4,2,3,4,5,1,2,3,3))
colnames(data2) <- c("A", "B")
hist(data2$A)
hist(data2$B)