Dataframes in a list; adding a new variable with name of dataframe

后端 未结 4 1614
猫巷女王i
猫巷女王i 2020-12-14 03:39

I have a list of dataframes which I eventually want to merge while maintaining a record of their original dataframe name or list index. This will allow me to subset etc acro

4条回答
  •  旧巷少年郎
    2020-12-14 03:54

    names() could work it it had names, but you didn't give it any. It's an unnamed list. You will need ti use numeric indices:

    > for(i in 1:length(mylist) ){ mylist[[i]] <- cbind(mylist[[i]], id=rep(i, nrow(mylist[[i]]) ) ) }
    > mylist
    [[1]]
      x  y id
    1 1 11  1
    2 2 12  1
    3 3 13  1
    4 4 14  1
    5 5 15  1
    
    [[2]]
      x  y id
    1 1 11  2
    2 2 12  2
    3 3 13  2
    4 4 14  2
    5 5 15  2
    

提交回复
热议问题