I have physical measurements that I have put into a csv, then imported into R for calcs. I found the mean and std of these measurements and assigned std with value NaN to zero.
The first column contains the (unique) names of different materials, and I'm trying to assign this column as the row names. This comes up with an error of: invalid 'row.names' length, despite the fact I'm using the first column from the dataframe.
data <- read.csv("my.csv", header = TRUE, fileEncoding = "UTF-8-BOM")
data <- data.frame(data) %>% select_if(~sum(!is.na(.)) > 0)
data_ave <- data %>% group_by(material) %>% summarise(mean_width = mean(width_cm), std_width = sd(width_cm))
data_ave$std[is.nan(data_ave$std_width)] <- 0
rownames(data_ave) <- data_ave[,1]
Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
Also:
nrow(data_ave[,1])==nrow(data_ave)
TRUE
I've used a similar method successfully for another dataframe, then simply removed the first column. How can they have the same number of rows, and for the length of row.names to be the problem here?
Cheers for the help
Most probably that is because data_ave
is a tibble. You cannot set rownames on tibble also data_ave[, 1]
would return a tibble back unlike dataframes which will return a vector.
This can be demonstrated with this example.
library(dplyr)
data_ave <- mtcars %>%
group_by(cyl) %>%
summarise(mpg = mean(mpg))
rownames(data_ave) <- data_ave[, 1]
Error in
.rowNamesDF<-
(x, value = value) : invalid 'row.names' length In addition: Warning message: Setting row names on a tibble is deprecated.
Solution is to change data_ave
to dataframe.
data_ave <- data.frame(data_ave)
rownames(data_ave) <- data_ave[, 1]
data_ave
# cyl mpg
#4 4 26.66364
#6 6 19.74286
#8 8 15.10000