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 nrowgives me the answer I'm looking for, but I don't understand why.

1

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
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like