why nrow(dataframe) and length(dataframe) in r give different results?
I have a ordered data frame and want to know the number of the last row.
data_ranking <- reduced_data[order(reduced_data$outcome,reduced_data$hospital,na.last=NA),] nobs <- nrow(data_ranking)
gives me different results of
data_ranking <- reduced_data[order(reduced_data$outcome,reduced_data$hospital,na.last=NA),] nobs <- length(data_ranking)
I would like to understand why is that. It seems that nrow
gives me the answer I'm looking for, but I don't understand why.
1 Answer
data frames are essentially lists where each element has the same length.
Each element of the list is a column, hence length
gives you the length of the list, usually the number of columns.
nrow
will give you the number of rows, ncol
(or length
) the number of columns.
The obvious equivalence of columns and list lengths gets messy once we have nonstandard structures within the data.frame (eg. matrices) and
x <- data.frame(y=1:5, z = matrix(1:10,ncol=2)) ncol(x) # 3 length(x) # 3 x1 <- data.frame(y=1:5, z = I(matrix(1:10,ncol=2))) ncol(x1) # 2 length(x) # 2
3ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoam5rYGqGeYSOsJ%2ByZZ6nvLiwwK2Yn6qRorJurc2dZKWdnpzBqbDArZifqpGism61zWapZp%2BZq7JusMifnZ6qlaPBbr7ErKylrKM%3D