R:在循环中使用列表级别名称命名.csv

时间:2018-10-29 12:06:17

标签: r loops

短篇小说 我有一个循环遍历列表的各个层次,创建一个新的数据集,并将输出写为.csv。我可以使用列表级别编号来命名.csv文件。相反,我想用级别的名称来命名.csv

详细信息

我有一个列表(yymm)。 Dput dput(head(yymm))如下:

list(`2003.1` = structure(list(cell_id = c(2730L, 2767L, 3067L, 
3074L, 3103L, 3107L, 3108L, 3108L, 3111L, 3139L, 3140L, 3140L, 
3142L, 3143L, 3143L, 3178L, 3179L, 3180L, 3181L, 3181L, 3181L, 
3184L, 3185L, 3216L, 3217L, 3218L, 3219L, 3224L, 3260L, 3261L, 
3263L, 3263L, 3264L, 3265L, 3265L, 3302L, 3303L, 3304L, 3306L, 
3348L, 3349L, 3350L, 3389L, 3391L, 3392L, 3392L, 3395L, 3437L, 
3438L, 3499L, 3499L, 3567L, 3631L, 3632L, 3633L, 3635L, 3636L, 
3698L, 3701L, 3701L), year = c(2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L), month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L)), row.names = c(281L, 310L, 721L, 744L, 787L, 814L, 823L, 
824L, 833L, 875L, 885L, 886L, 905L, 917L, 918L, 1039L, 1048L, 
1062L, 1070L, 1071L, 1072L, 1108L, 1115L, 1182L, 1193L, 1202L, 
1214L, 1271L, 1345L, 1360L, 1382L, 1383L, 1391L, 1397L, 1398L, 
1488L, 1498L, 1507L, 1516L, 1639L, 1644L, 1646L, 1735L, 1750L, 
1758L, 1759L, 1778L, 1911L, 1923L, 2131L, 2132L, 2448L, 2715L, 
2724L, 2738L, 2765L, 2779L, 3025L, 3052L, 3053L), class = "data.frame"), 
    `2004.1` = structure(list(cell_id = c(3101L, 3102L, 3103L, 
    3103L, 3104L, 3107L, 3109L, 3137L, 3138L, 3138L, 3139L, 3141L, 
    3141L, 3142L, 3144L, 3146L, 3146L, 3173L, 3174L, 3175L, 3175L, 
    3177L, 3178L, 3179L, 3180L, 3181L, 3181L, 3182L, 3183L, 3184L, 
    3184L, 3184L, 3186L, 3216L, 3217L, 3217L, 3218L, 3220L, 3221L, 
    3222L, 3222L, 3256L, 3256L, 3257L, 3259L, 3260L, 3261L, 3262L, 
    3264L, 3296L, 3297L, 3300L, 3301L, 3306L, 3307L, 3307L, 3337L, 
    3338L, 3339L, 3340L, 3343L, 3344L, 3345L, 3345L, 3348L, 3350L, 
    3380L, 3381L, 3383L, 3383L, 3383L, 3384L, 3385L, 3389L, 3390L, 
    3390L, 3391L, 3391L, 3392L, 3393L, 3394L, 3394L, 3427L, 3427L, 
    3428L, 3432L, 3433L, 3433L, 3433L, 3434L, 3435L, 3436L, 3441L, 
    3442L, 3443L, 3486L, 3487L, 3489L, 3490L, 3490L, 3492L, 3493L, 
    3496L, 3497L, 3498L, 3499L, 3500L, 3500L, 3501L, 3552L, 3553L, 
    3553L, 3554L, 3558L, 3560L, 3561L, 3563L, 3566L, 3568L, 3568L, 
    3621L, 3621L, 3623L, 3630L, 3633L, 3636L, 3695L, 3953L, 4004L, 
    NA), year = c(2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
    2004L, 2004L, 2004L, 2004L, 2004L, 2004L), month = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(772L, 
    778L, 788L, 789L, 797L, 815L, 827L, 859L, 869L, 870L, 876L, 
    897L, 898L, 906L, 921L, 934L, 935L, 980L, 993L, 1004L, 1005L, 
    1026L, 1040L, 1049L, 1063L, 1073L, 1074L, 1085L, 1098L, 1109L, 
    1110L, 1111L, 1117L, 1183L, 1194L, 1195L, 1203L, 1226L, 1237L, 
    1252L, 1253L, 1307L, 1308L, 1320L, 1336L, 1346L, 1361L, 1369L, 
    1392L, 1441L, 1452L, 1475L, 1481L, 1517L, 1520L, 1521L, 1532L, 
    1546L, 1561L, 1574L, 1602L, 1614L, 1622L, 1623L, 1640L, 1647L, 
    1662L, 1666L, 1676L, 1677L, 1678L, 1685L, 1694L, 1736L, 1747L, 
    1748L, 1751L, 1752L, 1760L, 1767L, 1771L, 1772L, 1821L, 1822L, 
    1828L, 1863L, 1871L, 1872L, 1873L, 1882L, 1892L, 1903L, 1945L, 
    1948L, 1954L, 2032L, 2044L, 2055L, 2059L, 2060L, 2073L, 2082L, 
    2109L, 2118L, 2121L, 2133L, 2143L, 2144L, 2151L, 2348L, 2356L, 
    2357L, 2363L, 2388L, 2399L, 2405L, 2419L, 2441L, 2454L, 2455L, 
    2676L, 2677L, 2686L, 2709L, 2741L, 2780L, 3002L, 4636L, 5066L, 
    11458L), class = "data.frame"), `2005.1` = structure(list(
        cell_id = c(3221L, 3224L, 3224L, 3264L, 3306L, 3349L, 
        3439L, 3440L, 3442L, 3501L, 3566L, 3568L, 3632L, 3634L, 
        3634L, 3635L, 3635L, 3635L, 3699L), year = c(2005L, 2005L, 
        2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
        2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
        2005L), month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(1238L, 
    1272L, 1273L, 1393L, 1518L, 1645L, 1929L, 1935L, 1949L, 2152L, 
    2442L, 2456L, 2725L, 2751L, 2752L, 2766L, 2767L, 2768L, 3031L
    ), class = "data.frame"), `2006.1` = structure(list(cell_id = c(3255L, 
    3256L, 3256L, 3294L, 3297L, 3304L, 3307L, 3339L, 3339L, 3340L, 
    3340L, 3341L, 3343L, 3346L, 3347L, 3348L, 3350L, 3381L, 3385L, 
    3387L, 3387L, 3389L, 3392L, 3394L, 3431L, 3432L, 3433L, 3435L, 
    3435L, 3437L, 3437L, 3493L, 3494L, 3495L, 3496L, 3496L, 3498L, 
    3498L, 3499L, 3500L, 3561L, 3561L, 3564L, 3566L, 3567L, 3629L, 
    3631L, 3632L, 3634L, 3635L, 3698L, 3699L, 3699L, 3700L), 
        year = c(2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
        2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
        2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
        2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
        2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
        2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
        2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L), month = c(1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(1299L, 
    1309L, 1310L, 1419L, 1455L, 1508L, 1522L, 1563L, 1564L, 1575L, 
    1576L, 1588L, 1604L, 1627L, 1636L, 1641L, 1648L, 1668L, 1695L, 
    1714L, 1715L, 1738L, 1761L, 1774L, 1856L, 1864L, 1874L, 1893L, 
    1894L, 1912L, 1913L, 2084L, 2093L, 2104L, 2110L, 2111L, 2122L, 
    2123L, 2134L, 2145L, 2407L, 2408L, 2428L, 2443L, 2449L, 2702L, 
    2718L, 2726L, 2753L, 2769L, 3026L, 3032L, 3033L, 3042L), class = "data.frame"), 
    `2001.2` = structure(list(cell_id = c(4978L, 4979L, 4980L, 
    4980L, 4981L, 4982L), year = c(2001L, 2001L, 2001L, 2001L, 
    2001L, 2001L), month = c(2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(10237L, 
    10239L, 10244L, 10245L, 10250L, 10262L), class = "data.frame"), 
    `2005.2` = structure(list(cell_id = c(3302L, 3344L, 3345L, 
    3389L), year = c(2005L, 2005L, 2005L, 2005L), month = c(2L, 
    2L, 2L, 2L)), row.names = c(1490L, 1615L, 1624L, 1737L), class = "data.frame"))

Str(head(yymm))如下

 List of 6
     $ 2003.1:'data.frame': 60 obs. of  3 variables:
      ..$ cell_id: int [1:60] 2730 2767 3067 3074 3103 3107 3108 3108 3111 3139 ...
      ..$ year   : int [1:60] 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 ...
      ..$ month  : int [1:60] 1 1 1 1 1 1 1 1 1 1 ...
     $ 2004.1:'data.frame': 130 obs. of  3 variables:
      ..$ cell_id: int [1:130] 3101 3102 3103 3103 3104 3107 3109 3137 3138 3138 ...
      ..$ year   : int [1:130] 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 ...
      ..$ month  : int [1:130] 1 1 1 1 1 1 1 1 1 1 ...
     $ 2005.1:'data.frame': 19 obs. of  3 variables:
      ..$ cell_id: int [1:19] 3221 3224 3224 3264 3306 3349 3439 3440 3442 3501 ...
      ..$ year   : int [1:19] 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 ...
      ..$ month  : int [1:19] 1 1 1 1 1 1 1 1 1 1 ...
     $ 2006.1:'data.frame': 54 obs. of  3 variables:
      ..$ cell_id: int [1:54] 3255 3256 3256 3294 3297 3304 3307 3339 3339 3340 ...
      ..$ year   : int [1:54] 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 ...
      ..$ month  : int [1:54] 1 1 1 1 1 1 1 1 1 1 ...
     $ 2001.2:'data.frame': 6 obs. of  3 variables:
      ..$ cell_id: int [1:6] 4978 4979 4980 4980 4981 4982
      ..$ year   : int [1:6] 2001 2001 2001 2001 2001 2001
      ..$ month  : int [1:6] 2 2 2 2 2 2
     $ 2005.2:'data.frame': 4 obs. of  3 variables:
      ..$ cell_id: int [1:4] 3302 3344 3345 3389
      ..$ year   : int [1:4] 2005 2005 2005 2005
      ..$ month  : int [1:4] 2 2 2 2

我也有一个数据框(cell)。 dput(head(cell))如下:

structure(list(longitude = c(-549722.741398157, -524722.741398157, 
-499722.741398157, -474722.741398157, -449722.741398157, -424722.741398157
), latitude = c(2483720.25484064, 2483720.25484064, 2483720.25484064, 
2483720.25484064, 2483720.25484064, 2483720.25484064), cell_id = c(1, 
2, 3, 4, 5, 6)), row.names = c("325", "326", "327", "328", "329", 
"330"), class = "data.frame")

Str(head(cell))如下:

'data.frame':   6 obs. of  3 variables:
 $ longitude: num  -549723 -524723 -499723 -474723 -449723 ...
 $ latitude : num  2483720 2483720 2483720 2483720 2483720 ...
 $ cell_id  : num  1 2 3 4 5 6

我想为yymm中的每个级别创建一个单元格副本,然后删除cell_id的级别中出现yymm的值的所有行。最后,我想将输出写入.csv,并为每个.csv提供与要处理的yymm级别相同的名称。

我写了一个几乎可以正常工作的简单循环:

no_timeslices <- length(timeslice) 
for (i in 1:no_timeslices){
  timeslice <- yymm[[i]]
  timeslice <- as.data.frame(timeslice)
  time_cell <- cell
  time_cell <- time_cell[!time_cell$cell_id %in% timeslice$cell_id, , drop = FALSE]
  write.csv(time_cell, paste0(i, ".csv"), row.names = FALSE)
}

问题 在此代码中,生成的.csv文件是级别号为[i]的名称。我不知道如何用级别名称来写每个.csv…… 如果有人对如何执行此操作有任何想法,我将非常感激。

非常感谢!

1 个答案:

答案 0 :(得分:1)

您想要data.frame中每个yymm的名称。因此,您可以使用names使用标签创建矢量:

names(lapply(yymm, names))[i]