简体   繁体   中英

dataframes in a list, how to change the index?

My data is split up in many files, which I need now to merge. It's my first time in R , as the source I work with does not permit Python. I understand that one can collect the dataframes as

for (year in seq) LL[[year]] <- read.dta(paste("filenameStart", year, ".dta", sep="") )

Nicely, I then have

> summary(LL)
     Length Class      Mode
1993 54     data.frame list
1994 54     data.frame list
1995 54     data.frame list
1996 54     data.frame list
1997 54     data.frame list
1998 54     data.frame list
1999 54     data.frame list
2000 54     data.frame list
2001 54     data.frame list
2002 54     data.frame list
2003 54     data.frame list
2004 54     data.frame list
2005 54     data.frame list
2006 54     data.frame list
2007 54     data.frame list
2008 54     data.frame list
2009 54     data.frame list
2010 54     data.frame list

However, I fail to understand what the first column is. It can't be the index, since I fail to do

> LL[1993]
$<NA>
NULL

and have to access them using LL[1] etc. Is there a way to use the year as the index, as the summary seems to imply? Or am I doing this completely wrong, " un-R-ish "?

Looking at the help page at ?dimnames , you would come across the following in the "Value" section:

The dimnames of a matrix or array can be NULL (which is not stored) or a list of the same length as dim(x) . If a list, its components are either NULL or a character vector with positive length of the appropriate dimension of x. The list can have names. It is possible that all components are NULL : such dimnames may get converted to NULL .

For the "data.frame" method both dimnames are character vectors, and the rownames must contain no duplicates nor missing values.

Thus, to access the values, you need to treat them as character vectors.

LL[["1993"]]

If you don't treat them as character vectors, L[[1993]] will assume you are trying to get the 1993rd list element. If that doesn't exist, you will get a subscript out of bounds error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM