我有几个数据框如下:gkz.01.1999,gkz.01.2000 ...,gkz.02.1999,gkz.02.2000 ......数据如下所示:
col1 col2 col3 col4
1 under 1 year 14091 13394 27485
2 1 year 14476 13802 28278
3 2 years 15420 14336 29756
4 3 years 15285 14437 29722
5 4 years 14704 13901 28605
6 5 years 14966 14016 28982
如何遍历所有数据帧(或使用apply)并向每个数据帧添加两个新列,其中第一个新列(gkz)的值等于数据帧名称的前两位数字第二个新列(年份)中的值是否等于数据框名称的最后4位数?例如,对于数据框gkz.01.1999:
col1 col2 col3 col4 gkz year
1 under 1 year 14091 13394 27485 01 1999
2 1 year 14476 13802 28278 01 1999
3 2 years 15420 14336 29756 01 1999
4 3 years 15285 14437 29722 01 1999
5 4 years 14704 13901 28605 01 1999
6 5 years 14966 14016 28982 01 1999
提前致谢。
答案 0 :(得分:1)
我们将data.frame
转换为list
。
#get the objects that start with 'gkz' as strings
nm1 <- ls(pattern = "gkz\\.\\d+")
#use mget to get the values of the objects in a list
lst <- mget(nm1)
#extract the numbers that follow the gkz using sub
nm2 <- sub("^[^.]+\\.([^.]+).*", "\\1", nm1)
#extract the last 4 numbers with sub
nm3 <- sub(".*\\.(\\d+)$", "\\1", nm1)
#create new columns in the list of data.frame with Map
lst1 <- Map(cbind, lst, gkz = nm2, year = as.integer(nm3))