循环遍历

时间:2016-11-19 15:03:19

标签: r data.table

所有,我正在处理数据集,如下所示, 我想创建新的列,这些列是感兴趣的变量。 我目前的代码如下:

library(data.table)
setDF(final)
final.consec<-final[final$priva.consec.count>4 | final$unpriva.consec.count>4,]
interesting.vars<-c("IR", "sales","totalasset","GM","Export","Leverage","ROA")
for (i in interesting.vars) {
          #i=as.name(i)
          setDT(final.consec)[,paste("L.1.",i):=shift(i,n=1),firmid]
          setDT(final.consec)[,paste("L.2.",i):=shift(i,n=2),firmid]
}

但这将产生所有的NA而不是我想要的滞后和线索

我搜索谷歌和这里,有些人建议使用

shift(final.consec[[i]],n=1)

shift(as.name(i),n=1)

但他们都没有工作。

final = structure(list(year = c(2002, 2003, 2004, 2005, 2006, 2007, 2003, 
2004, 2005, 2006, 2007, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 
2005, 2006, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 
2007), firmid = c("016016226", "016016226", "016016226", "016016226", 
"016016226", "016016226", "101100393", "101100393", "101100393", 
"101100393", "101100393", "101100481", "101100481", "101100481", 
"101100481", "101100481", "101100481", "101100481", "101100481", 
"101100481", "101100588", "101100588", "101100588", "101100588", 
"101100588", "101100588", "101100588", "101100588", "101100588", 
"101100588"), provinceid = c(610000, 610000, 610000, 610000, 
610000, 610000, 110000, 110000, 110000, 110000, 110000, 110000, 
110000, 110000, 110000, 110000, 110000, 110000, 110000, 110000, 
110000, 110000, 110000, 110000, 110000, 110000, 110000, 110000, 
110000, 110000), industrycode2 = c(3100, 3100, 3100, 3100, 3100, 
3100, 3100, 3100, 3100, 3100, 3100, 2300, 2300, 2300, 2300, 2300, 
2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 
2300, 2300, 2300), sales = c(1998, 2995, 2902, 5006, 5291, 6241, 
98670, 60643, 79075, 93600, 55591, 6525, 7984, 7654, 7432, 7596, 
8410, 7359, 5456, 6645, 10355, 9498, 8967, 11551, 13647, 14008, 
20647, 21437, 20155, 14535), cogs = c(1602, 898, 1301, 1453, 
2615, 2835, 66143, 40037, 55971, 66167, 46862, 7430, 7487, 6820, 
7482, 7436, 7905, 7243, 6560, 6605, 8126, 7868, 8722, 8700, 11222, 
11521, 18282, 20322, 17859, 12913), inventory = c(1034, 1012, 
896, 653, 1685, 2036, 16690, 29623, 28770, 27124, 6712, 0, 0, 
0, 93, 21, 79, 78, 18, 797, 1778, 1588, 2785, 3414, 4435, 4147, 
5874, 5339, 3991, 3345), fixedasset = c(29577, 30856, 28250, 
28245, 28168, 27919, 77646, 101803, 94984, 84585, 54935, 18418, 
17192, 17518, 13219, 16600, 14132, 20737, 21332, 25192, 15423, 
7028, 5513, 11679, 9364, 35778, 59804, 65344, 69266, 73777), 
    totalasset = c(74278, 74358, 74000, 74006, 74011, 73913, 
    273642, 321636, 357791, 373095, 340400, 31312, 31242, 35461, 
    42755, 47784, 43798, 41565, 40220, 45753, 30114, 31164, 31347, 
    31526, 32244, 61842, 87170, 88730, 98821, 104000), stateshare = c(1, 
    1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1), foreignshare = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), privateshare = c(0, 0, 0, 0, 0, 
    0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0), stateown = c(1, 1, 1, 1, 1, 1, 1, 1, 0, 
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1), foreignown = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), privateown = c(0, 
    0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), mixown = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0), stateonly = c(1, 1, 1, 1, 1, 1, 0, 0, 
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1), mixonly = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), foreignonly = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), privateonly = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), gs = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1), gm = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), gf = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), privatize = c(0, 0, 0, 0, 0, 0, 
    0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), IR = c(0.645443201065063, 1.12694883346558, 
    0.688700973987579, 0.449414998292923, 0.644359469413757, 
    0.718165755271912, 0.25233206152916, 0.739890575408936, 0.514016211032867, 
    0.409932434558868, 0.143229052424431, 0.0191550496965647, 
    0.0137506183236837, 0.0117767816409469, 0.0124298315495253, 
    0.00526109151542187, 0.00999367516487837, 0.0107690179720521, 
    0.00274390238337219, 0.120666161179543, 0.218803837895393, 
    0.201830193400383, 0.319307506084442, 0.392413794994354, 
    0.395205855369568, 0.3599514067173, 0.321299642324448, 0.262720197439194, 
    0.223472759127617, 0.259041279554367), GM = c(0.24719101190567, 
    0.807359278202057, 0.702536523342133, 0.714905440807343, 
    0.717204809188843, 0.731220781803131, 0.491767823696136, 
    0.514673948287964, 0.412785202264786, 0.41460245847702, 0.186270326375961, 
    -0.0678684562444687, 0.0663817301392555, 0.122287392616272, 
    -0.00668270513415337, 0.0215169452130795, 0.0638836175203323, 
    0.016015462577343, 0.00807300768792629, 0.014724993146956, 
    0.274304687976837, 0.207168281078339, 0.0280898883938789, 
    0.327701151371002, 0.216093391180038, 0.215866684913635, 
    0.129362210631371, 0.0548666454851627, 0.12856262922287, 
    0.125609844923019), CI = c(0.398193269968033, 0.414965450763702, 
    0.381756752729416, 0.381658256053925, 0.380592077970505, 
    0.377727866172791, 0.283750295639038, 0.316516190767288, 
    0.265473425388336, 0.226711690425873, 0.161383673548698, 
    0.588208973407745, 0.550284862518311, 0.494007498025894, 
    0.309180200099945, 0.347396612167358, 0.322663128376007, 
    0.498905330896378, 0.530382871627808, 0.550608694553375, 
    0.512153804302216, 0.225516617298126, 0.175870105624199, 
    0.370456129312515, 0.290410608053207, 0.578538835048676, 
    0.68606173992157, 0.728544950485229, 0.700923919677734, 0.709394216537476
    ), WACC = c(0.000888553797267377, 0.000511041202116758, 0.000527027004864067, 
    0.000459422240965068, 0.000513437204062939, 0.000608823902439326, 
    0.0238377153873444, 0.030428808182478, 0.0118085695430636, 
    0.0155402785167098, 0.00844594556838274, -0.0610947869718075, 
    -0.0143076628446579, -0.0111107975244522, 0.00177756987977773, 
    -0.0173279754817486, -0.0689300894737244, -0.0779983177781105, 
    -0.0916144475340843, -0.0209603756666183, -0.00126187154091895, 
    0.000481324619613588, 0.000191405866644345, 0.000348918343661353, 
    0.00533432560041547, -3.23404929076787e-05, -0.000206493074074388, 
    -0.0318719707429409, -0.0211898274719715, 0.00500000035390258
    ), Salesgrowth = c(NA, 0.404797554016113, -0.301691830158234, 
    0.210879027843475, 0.648279845714569, 0.0701394379138947, 
    NA, -0.486776739358902, 0.265392541885376, 0.168633610010147, 
    -0.496754705905914, NA, 0.201798588037491, -0.042211152613163, 
    -0.029433386400342, 0.0218267906457186, 0.101799681782722, 
    -0.133497416973114, -0.299208134412766, 0.197148770093918, 
    NA, -0.0863882452249527, -0.0575300790369511, 0.253220856189728, 
    0.166747704148293, 0.0261088777333498, 0.3879414498806, 0.0375483706593513, 
    -0.0616660043597221, -0.326892852783203), Export = c(0, 0, 
    0, 0, 0, 0, 0.0718493312597275, 0.0859285816550255, 0.113112106919289, 
    0.287075728178024, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), Leverage = c(10.397575378418, 10.444974899292, 
    11.0325202941895, 10.0521974563599, 1.022047996521, 1.01452708244324, 
    1.45040833950043, 1.67869281768799, 1.03448712825775, 1.11970138549805, 
    0.892383217811584, 1.10642445087433, 1.03876268863678, 1.37419652938843, 
    1.84710657596588, 3.55129051208496, 4.52725887298584, 7.83046531677246, 
    10.0521974563599, 9.32586765289307, 0.210126578807831, 0.234657898545265, 
    0.242646470665932, 0.251528382301331, 0.28160896897316, 1.45746076107025, 
    2.464350938797, 2.97518038749695, 3.88535690307617, 0.26302495598793
    ), Current = c(1.55197286605835, 1.69315719604492, 2.29052948951721, 
    2.24755930900574, 2.20689654350281, 2.10811376571655, 1.25970602035522, 
    1.65375781059265, 1.66235971450806, 1.39208269119263, 1.29157769680023, 
    0.969440996646881, 0.790197372436523, 0.650628983974457, 
    0.69122976064682, 1.09663212299347, 1.12163543701172, 1.6644686460495, 
    1.97333335876465, 2.0823986530304, 0.435712337493896, 0.577177941799164, 
    0.511789321899414, 0.731555223464966, 0.587089836597443, 
    2.40473389625549, 2.29052948951721, 2.24755930900574, 2.20689654350281, 
    1.11392271518707), Cover = c(0.395348846912384, 0.260869562625885, 
    0.209302321076393, 0.0377358496189117, 0.00228310492821038, 
    0.00416666688397527, 0.952529191970825, 0.885167479515076, 
    0.762962937355042, 0.727603793144226, -0.188469097018242, 
    0.00704225338995457, 0.0597014911472797, 0.0273972600698471, 
    -0.0126262623816729, 0.0230215825140476, 0.0177725125104189, 
    0.00775716686621308, -0.00155763234943151, 0.0120259020477533, 
    0.0303643718361855, 0, 0.00171057134866714, 0.00918484479188919, 
    0.389791190624237, -0.0153139354661107, -0.0214592274278402, 
    -0.00146555935498327, 0.00122399022802711, -0.00661703897640109
    ), Bank = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1), Inctaxrate = c(0, 
    0, 0.0666666701436043, 0, 0, 0, 0, 0.0026208502240479, 0.0247787609696388, 
    0.0154994260519743, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.142857149243355, 
    0.200000002980232, 0.181818187236786, 0.335363984107971, 
    0.333333343267441, 0.333333343267441, 0.33047690987587, 0, 
    0, 0), ROA = c(0.000659683893900365, 0.000430350453825668, 
    0.000405405415222049, 0.00043239738442935, 0.000499925692565739, 
    0.000595294462982565, 0.0148917194455862, 0.021353330463171, 
    0.00473740277811885, 0.00933810416609049, 0.00366333732381463, 
    -0.0607434846460819, -0.0158440563827753, -0.0109415976330638, 
    0.00189451524056494, -0.0169931352138519, -0.067902646958828, 
    -0.077444963157177, -0.0968421399593353, -0.0206762403249741, 
    0.000232450023759156, 0.000481324619613588, 0.000350910762790591, 
    9.51595502556302e-05, 0.000124054087791592, 0.000129361927974969, 
    2.29436736844946e-05, -0.0319057814776897, -0.0211797095835209, 
    0.00492307683452964), ROS = c(0.02452452480793, 0.0106844743713737, 
    0.0135440183803439, 0.0117001831531525, 0.00707456981763244, 
    0.00784313771873713, 0.0412992797791958, 0.113252975046635, 
    0.0214353464543819, 0.037222221493721, 0.0224316883832216, 
    -0.291494250297546, -0.0619989968836308, -0.0506924502551556, 
    0.0108988154679537, -0.106898367404938, -0.241563051939011, 
    -0.216171622276306, -0.163818180561066, -0.142362684011459, 
    0.000676001945976168, 0.00157927989494056, 0.00122672016732395, 
    0.000259717780863866, 0.000293104705633596, 0.000571102253161371, 
    9.68663734965958e-05, -0.132061392068863, -0.103845201432705, 
    0.0352253168821335), num_ID = c(66, 66, 66, 66, 66, 66, 95, 
    95, 95, 95, 95, 96, 96, 96, 96, 96, 96, 96, 96, 96, 100, 
    100, 100, 100, 100, 100, 100, 100, 100, 100), priva = c(0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    priva.year = c(0, 0, 0, 0, 0, 0, 2005, 2005, 2005, 2005, 
    2005, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0), relative.year = c(NA, NA, NA, NA, NA, NA, -2, -1, 
    0, 1, 2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA), priva.consec.count = c(0L, 0L, 
    0L, 0L, 0L, 0L, 5L, 5L, 5L, 5L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), unpriva.consec.count = c(5, 
    5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9), `L.1. IR` = c(NA, 0.645443201065063, 
    1.12694883346558, 0.688700973987579, 0.449414998292923, 0.644359469413757, 
    NA, 0.25233206152916, 0.739890575408936, 0.514016211032867, 
    0.409932434558868, NA, 0.0191550496965647, 0.0137506183236837, 
    0.0117767816409469, 0.0124298315495253, 0.00526109151542187, 
    0.00999367516487837, 0.0107690179720521, 0.00274390238337219, 
    NA, 0.218803837895393, 0.201830193400383, 0.319307506084442, 
    0.392413794994354, 0.395205855369568, 0.3599514067173, 0.321299642324448, 
    0.262720197439194, 0.223472759127617), `L.2. IR` = c(NA, 
    NA, 0.645443201065063, 1.12694883346558, 0.688700973987579, 
    0.449414998292923, NA, NA, 0.25233206152916, 0.739890575408936, 
    0.514016211032867, NA, NA, 0.0191550496965647, 0.0137506183236837, 
    0.0117767816409469, 0.0124298315495253, 0.00526109151542187, 
    0.00999367516487837, 0.0107690179720521, NA, NA, 0.218803837895393, 
    0.201830193400383, 0.319307506084442, 0.392413794994354, 
    0.395205855369568, 0.3599514067173, 0.321299642324448, 0.262720197439194
    ), `H.1. IR` = c(1.12694883346558, 0.688700973987579, 0.449414998292923, 
    0.644359469413757, 0.718165755271912, NA, 0.739890575408936, 
    0.514016211032867, 0.409932434558868, 0.143229052424431, 
    NA, 0.0137506183236837, 0.0117767816409469, 0.0124298315495253, 
    0.00526109151542187, 0.00999367516487837, 0.0107690179720521, 
    0.00274390238337219, 0.120666161179543, NA, 0.201830193400383, 
    0.319307506084442, 0.392413794994354, 0.395205855369568, 
    0.3599514067173, 0.321299642324448, 0.262720197439194, 0.223472759127617, 
    0.259041279554367, NA), `H.2. IR` = c(0.688700973987579, 
    0.449414998292923, 0.644359469413757, 0.718165755271912, 
    NA, NA, 0.514016211032867, 0.409932434558868, 0.143229052424431, 
    NA, NA, 0.0117767816409469, 0.0124298315495253, 0.00526109151542187, 
    0.00999367516487837, 0.0107690179720521, 0.00274390238337219, 
    0.120666161179543, NA, NA, 0.319307506084442, 0.392413794994354, 
    0.395205855369568, 0.3599514067173, 0.321299642324448, 0.262720197439194, 
    0.223472759127617, 0.259041279554367, NA, NA), `L.1. sales` = c(NA, 
    1998, 2995, 2902, 5006, 5291, NA, 98670, 60643, 79075, 93600, 
    NA, 6525, 7984, 7654, 7432, 7596, 8410, 7359, 5456, NA, 10355, 
    9498, 8967, 11551, 13647, 14008, 20647, 21437, 20155), `L.2. sales` = c(NA, 
    NA, 1998, 2995, 2902, 5006, NA, NA, 98670, 60643, 79075, 
    NA, NA, 6525, 7984, 7654, 7432, 7596, 8410, 7359, NA, NA, 
    10355, 9498, 8967, 11551, 13647, 14008, 20647, 21437), `H.1. sales` = c(2995, 
    2902, 5006, 5291, 6241, NA, 60643, 79075, 93600, 55591, NA, 
    7984, 7654, 7432, 7596, 8410, 7359, 5456, 6645, NA, 9498, 
    8967, 11551, 13647, 14008, 20647, 21437, 20155, 14535, NA
    ), `H.2. sales` = c(2902, 5006, 5291, 6241, NA, NA, 79075, 
    93600, 55591, NA, NA, 7654, 7432, 7596, 8410, 7359, 5456, 
    6645, NA, NA, 8967, 11551, 13647, 14008, 20647, 21437, 20155, 
    14535, NA, NA), `L.1. totalasset` = c(NA, 74278, 74358, 74000, 
    74006, 74011, NA, 273642, 321636, 357791, 373095, NA, 31312, 
    31242, 35461, 42755, 47784, 43798, 41565, 40220, NA, 30114, 
    31164, 31347, 31526, 32244, 61842, 87170, 88730, 98821), 
    `L.2. totalasset` = c(NA, NA, 74278, 74358, 74000, 74006, 
    NA, NA, 273642, 321636, 357791, NA, NA, 31312, 31242, 35461, 
    42755, 47784, 43798, 41565, NA, NA, 30114, 31164, 31347, 
    31526, 32244, 61842, 87170, 88730), `H.1. totalasset` = c(74358, 
    74000, 74006, 74011, 73913, NA, 321636, 357791, 373095, 340400, 
    NA, 31242, 35461, 42755, 47784, 43798, 41565, 40220, 45753, 
    NA, 31164, 31347, 31526, 32244, 61842, 87170, 88730, 98821, 
    104000, NA), `H.2. totalasset` = c(74000, 74006, 74011, 73913, 
    NA, NA, 357791, 373095, 340400, NA, NA, 35461, 42755, 47784, 
    43798, 41565, 40220, 45753, NA, NA, 31347, 31526, 32244, 
    61842, 87170, 88730, 98821, 104000, NA, NA), `L.1. GM` = c(NA, 
    0.24719101190567, 0.807359278202057, 0.702536523342133, 0.714905440807343, 
    0.717204809188843, NA, 0.491767823696136, 0.514673948287964, 
    0.412785202264786, 0.41460245847702, NA, -0.0678684562444687, 
    0.0663817301392555, 0.122287392616272, -0.00668270513415337, 
    0.0215169452130795, 0.0638836175203323, 0.016015462577343, 
    0.00807300768792629, NA, 0.274304687976837, 0.207168281078339, 
    0.0280898883938789, 0.327701151371002, 0.216093391180038, 
    0.215866684913635, 0.129362210631371, 0.0548666454851627, 
    0.12856262922287), `L.2. GM` = c(NA, NA, 0.24719101190567, 
    0.807359278202057, 0.702536523342133, 0.714905440807343, 
    NA, NA, 0.491767823696136, 0.514673948287964, 0.412785202264786, 
    NA, NA, -0.0678684562444687, 0.0663817301392555, 0.122287392616272, 
    -0.00668270513415337, 0.0215169452130795, 0.0638836175203323, 
    0.016015462577343, NA, NA, 0.274304687976837, 0.207168281078339, 
    0.0280898883938789, 0.327701151371002, 0.216093391180038, 
    0.215866684913635, 0.129362210631371, 0.0548666454851627), 
    `H.1. GM` = c(0.807359278202057, 0.702536523342133, 0.714905440807343, 
    0.717204809188843, 0.731220781803131, NA, 0.514673948287964, 
    0.412785202264786, 0.41460245847702, 0.186270326375961, NA, 
    0.0663817301392555, 0.122287392616272, -0.00668270513415337, 
    0.0215169452130795, 0.0638836175203323, 0.016015462577343, 
    0.00807300768792629, 0.014724993146956, NA, 0.207168281078339, 
    0.0280898883938789, 0.327701151371002, 0.216093391180038, 
    0.215866684913635, 0.129362210631371, 0.0548666454851627, 
    0.12856262922287, 0.125609844923019, NA), `H.2. GM` = c(0.702536523342133, 
    0.714905440807343, 0.717204809188843, 0.731220781803131, 
    NA, NA, 0.412785202264786, 0.41460245847702, 0.186270326375961, 
    NA, NA, 0.122287392616272, -0.00668270513415337, 0.0215169452130795, 
    0.0638836175203323, 0.016015462577343, 0.00807300768792629, 
    0.014724993146956, NA, NA, 0.0280898883938789, 0.327701151371002, 
    0.216093391180038, 0.215866684913635, 0.129362210631371, 
    0.0548666454851627, 0.12856262922287, 0.125609844923019, 
    NA, NA), `L.1. Export` = c(NA, 0, 0, 0, 0, 0, NA, 0.0718493312597275, 
    0.0859285816550255, 0.113112106919289, 0.287075728178024, 
    NA, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ), `L.2. Export` = c(NA, NA, 0, 0, 0, 0, NA, NA, 0.0718493312597275, 
    0.0859285816550255, 0.113112106919289, NA, NA, 0, 0, 0, 0, 
    0, 0, 0, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0), `H.1. Export` = c(0, 
    0, 0, 0, 0, NA, 0.0859285816550255, 0.113112106919289, 0.287075728178024, 
    1, NA, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, NA), `H.2. Export` = c(0, 0, 0, 0, NA, NA, 0.113112106919289, 
    0.287075728178024, 1, NA, NA, 0, 0, 0, 0, 0, 0, 0, NA, NA, 
    0, 0, 0, 0, 0, 0, 0, 0, NA, NA), `L.1. Leverage` = c(NA, 
    10.397575378418, 10.444974899292, 11.0325202941895, 10.0521974563599, 
    1.022047996521, NA, 1.45040833950043, 1.67869281768799, 1.03448712825775, 
    1.11970138549805, NA, 1.10642445087433, 1.03876268863678, 
    1.37419652938843, 1.84710657596588, 3.55129051208496, 4.52725887298584, 
    7.83046531677246, 10.0521974563599, NA, 0.210126578807831, 
    0.234657898545265, 0.242646470665932, 0.251528382301331, 
    0.28160896897316, 1.45746076107025, 2.464350938797, 2.97518038749695, 
    3.88535690307617), `L.2. Leverage` = c(NA, NA, 10.397575378418, 
    10.444974899292, 11.0325202941895, 10.0521974563599, NA, 
    NA, 1.45040833950043, 1.67869281768799, 1.03448712825775, 
    NA, NA, 1.10642445087433, 1.03876268863678, 1.37419652938843, 
    1.84710657596588, 3.55129051208496, 4.52725887298584, 7.83046531677246, 
    NA, NA, 0.210126578807831, 0.234657898545265, 0.242646470665932, 
    0.251528382301331, 0.28160896897316, 1.45746076107025, 2.464350938797, 
    2.97518038749695), `H.1. Leverage` = c(10.444974899292, 11.0325202941895, 
    10.0521974563599, 1.022047996521, 1.01452708244324, NA, 1.67869281768799, 
    1.03448712825775, 1.11970138549805, 0.892383217811584, NA, 
    1.03876268863678, 1.37419652938843, 1.84710657596588, 3.55129051208496, 
    4.52725887298584, 7.83046531677246, 10.0521974563599, 9.32586765289307, 
    NA, 0.234657898545265, 0.242646470665932, 0.251528382301331, 
    0.28160896897316, 1.45746076107025, 2.464350938797, 2.97518038749695, 
    3.88535690307617, 0.26302495598793, NA), `H.2. Leverage` = c(11.0325202941895, 
    10.0521974563599, 1.022047996521, 1.01452708244324, NA, NA, 
    1.03448712825775, 1.11970138549805, 0.892383217811584, NA, 
    NA, 1.37419652938843, 1.84710657596588, 3.55129051208496, 
    4.52725887298584, 7.83046531677246, 10.0521974563599, 9.32586765289307, 
    NA, NA, 0.242646470665932, 0.251528382301331, 0.28160896897316, 
    1.45746076107025, 2.464350938797, 2.97518038749695, 3.88535690307617, 
    0.26302495598793, NA, NA), `L.1. ROA` = c(NA, 0.000659683893900365, 
    0.000430350453825668, 0.000405405415222049, 0.00043239738442935, 
    0.000499925692565739, NA, 0.0148917194455862, 0.021353330463171, 
    0.00473740277811885, 0.00933810416609049, NA, -0.0607434846460819, 
    -0.0158440563827753, -0.0109415976330638, 0.00189451524056494, 
    -0.0169931352138519, -0.067902646958828, -0.077444963157177, 
    -0.0968421399593353, NA, 0.000232450023759156, 0.000481324619613588, 
    0.000350910762790591, 9.51595502556302e-05, 0.000124054087791592, 
    0.000129361927974969, 2.29436736844946e-05, -0.0319057814776897, 
    -0.0211797095835209), `L.2. ROA` = c(NA, NA, 0.000659683893900365, 
    0.000430350453825668, 0.000405405415222049, 0.00043239738442935, 
    NA, NA, 0.0148917194455862, 0.021353330463171, 0.00473740277811885, 
    NA, NA, -0.0607434846460819, -0.0158440563827753, -0.0109415976330638, 
    0.00189451524056494, -0.0169931352138519, -0.067902646958828, 
    -0.077444963157177, NA, NA, 0.000232450023759156, 0.000481324619613588, 
    0.000350910762790591, 9.51595502556302e-05, 0.000124054087791592, 
    0.000129361927974969, 2.29436736844946e-05, -0.0319057814776897
    ), `H.1. ROA` = c(0.000430350453825668, 0.000405405415222049, 
    0.00043239738442935, 0.000499925692565739, 0.000595294462982565, 
    NA, 0.021353330463171, 0.00473740277811885, 0.00933810416609049, 
    0.00366333732381463, NA, -0.0158440563827753, -0.0109415976330638, 
    0.00189451524056494, -0.0169931352138519, -0.067902646958828, 
    -0.077444963157177, -0.0968421399593353, -0.0206762403249741, 
    NA, 0.000481324619613588, 0.000350910762790591, 9.51595502556302e-05, 
    0.000124054087791592, 0.000129361927974969, 2.29436736844946e-05, 
    -0.0319057814776897, -0.0211797095835209, 0.00492307683452964, 
    NA), `H.2. ROA` = c(0.000405405415222049, 0.00043239738442935, 
    0.000499925692565739, 0.000595294462982565, NA, NA, 0.00473740277811885, 
    0.00933810416609049, 0.00366333732381463, NA, NA, -0.0109415976330638, 
    0.00189451524056494, -0.0169931352138519, -0.067902646958828, 
    -0.077444963157177, -0.0968421399593353, -0.0206762403249741, 
    NA, NA, 0.000350910762790591, 9.51595502556302e-05, 0.000124054087791592, 
    0.000129361927974969, 2.29436736844946e-05, -0.0319057814776897, 
    -0.0211797095835209, 0.00492307683452964, NA, NA)), .Names = c("year", 
"firmid", "provinceid", "industrycode2", "sales", "cogs", "inventory", 
"fixedasset", "totalasset", "stateshare", "foreignshare", "privateshare", 
"stateown", "foreignown", "privateown", "mixown", "stateonly", 
"mixonly", "foreignonly", "privateonly", "gs", "gm", "gf", "privatize", 
"IR", "GM", "CI", "WACC", "Salesgrowth", "Export", "Leverage", 
"Current", "Cover", "Bank", "Inctaxrate", "ROA", "ROS", "num_ID", 
"priva", "priva.year", "relative.year", "priva.consec.count", 
"unpriva.consec.count", "L.1. IR", "L.2. IR", "H.1. IR", "H.2. IR", 
"L.1. sales", "L.2. sales", "H.1. sales", "H.2. sales", "L.1. totalasset", 
"L.2. totalasset", "H.1. totalasset", "H.2. totalasset", "L.1. GM", 
"L.2. GM", "H.1. GM", "H.2. GM", "L.1. Export", "L.2. Export", 
"H.1. Export", "H.2. Export", "L.1. Leverage", "L.2. Leverage", 
"H.1. Leverage", "H.2. Leverage", "L.1. ROA", "L.2. ROA", "H.1. ROA", 
"H.2. ROA"), row.names = c(NA, 30L), class = "data.frame")

==================================更新============ ===================== 根据弗兰克的回答,这个问题的解决方案是:

setDT(final.consec)#### with out this, below won'r work
interesting.vars<-c("IR", "sales","totalasset","GM","Export","Leverage","ROA")
##### lead 1 
anscols.Lead1 = paste("Lead.1", interesting.vars, sep="_")
final.consec[, (anscols.Lead1 ) := shift(.SD, 1,NA,type = "lead"),  .SDcols=interesting.vars,by = firmid]

0 个答案:

没有答案