When I create a dataframe, columns with the same values in every row automatically set to type "unknown" and it is not possible to change it.
Here is an example for your better understanding:
data <- data.frame(c(1,1,1,1), c(1:4), c(4:1))
colnames(data) <- c("Not Working", "Ok", "Ok")
The first column of such data frame ("Not Working") is filled with the same values (all 1). As you run the code, you'll notice that its type is "unknown", while "numeric" is automatically set for the others.
If you try to change it though, nothing works. For example:
data$`Not Working` <- as.numeric(data$`Not Working`)
data$`Not Working` <- as.numeric(as.character(data$`Not Working`))
You'll see that the column type is still the same using both string of code. Neither change anything using brackets instead of the dollar sign. This happens every time a column gets all its values equal. I also tried to turn the data frame into a matrix first and then into a data frame again, or to change columns into factors first (even if is meaningless for my specific kind of data) and then into numeric, but nothing works.
And although this is not a problem for a classic R script, it turns to be crucial when I try to knit the file, returning the following error:
"Error [...]: replacement has length zero"
After several test, I found out that the error is specific for the column type that should be numeric. I have R markdown and Latex properly installed, so it should be nothing about that.
Does anyone know why this happens and if there is a way to fix it? It looks like a bug or something, but I've already tried to update the program at the latest version but nothing changes.
Firstly you should not have two columns with the same name. I would recommend you use
tibbleto create data frames.If you want to change a data type of a column to something specific you can easily specify that.