将data.frames写入单独的文件

时间:2015-07-23 14:05:24

标签: r statistics

我有一个包含11时间序列的结果对象:

> dput(res)
structure(list(oenb_dependent = structure(c(1.02270390000001, 
-5.06831440000002, 0.665771299999989, 3.31613740000003, -2.15867040000001, 
-0.783362300000022, -0.22032089999999, 2.416144, -1.76254059999999, 
-0.156503700000002, -7.9803936, 9.45947149999999, -4.8104584, 
8.48271070000001, -6.18952620000002, 1.42885950000002, 1.4896459, 
-0.419852200000008, -5.1583964, 5.25022940000001, 1.0567102, 
-1.09233420000001, -1.58522979999999, 0.606193599999997, -0.375233499999993, 
2.50086640000001, -1.39997290000002, 2.28021660000002, -2.1468756, 
-1.4890328, -0.792543760000001, 3.21804705000001, -0.944078860000019, 
-0.278023159999989, -0.207530789999993, -1.12610048000001, 2.0883735, 
-0.742485400000007, 0.442037290000002, -1.48905938, 1.39644423999999, 
-3.89173769999999, 11.25665848, -9.22884035, 3.26856762, -0.00179540999998551, 
-2.39664325000003, 4.00455574000001, -5.60891295, 4.6556348, 
-4.40536951, 6.64234497, -7.34787318999999, 7.56303005999999, 
-8.23083674, 4.43247855, 1.31090412), .Tsp = c(2000.75, 2014.75, 
4), class = "ts"), gdp = structure(c(-103.900000000001, -271.599999999999, 
-284.200000000001, 34.2999999999993, -206, -253.099999999999, 
-116.799999999999, -169.900000000001, -63.6000000000004, -174.199999999999, 
-138.200000000001, -171.200000000001, -198.199999999999, -126.200000000001, 
-222.5, -8.19999999999891, -172.5, -169.1, -207.5, -114.6, -182.4, 
-43.7000000000007, 43.5, 166, 293.1, -30, -144.6, 16.8999999999996, 
-115.599999999999, -147.400000000001, -189.099999999999, -166.800000000001, 
-157.9, -108.700000000001, -150.9, -267.299999999999, -176.200000000001, 
-231.299999999999, -160.4, -251.5, -194.5, -186.300000000001, 
-193, -171.6, -191.699999999999, -254.4, -140.6, -126.300000000001, 
-66.6999999999989, -102.300000000001, -100.4, -133.1, -61.7999999999993, 
-1.10000000000036, -130.299999999999, -35.8000000000011, -114.9, 
-79.1000000000004), .Tsp = c(2000.5, 2014.75, 4), class = "ts"), 
    atx = structure(c(43.8598630000001, 296.910157, 22.9699700000001, 
    22.719932, -18.0900489999999, -304.469971, 128.03003, 49.1999900000001, 
    -311.47001, -114.390014, 183.710083, -267.380059, 56.1699759999999, 
    818.880004, 115.449952, 65.0600679999998, -405.610117, -262.829834, 
    355.199951, -138.44, 141.720029, -538.630127, -402.029907, 
    54.2100049999999, 1016.93001, 1175.389892, -177.23999, 747.070087999999, 
    14.3198050000001, 341.959961, -223.759766, -182.03, -595.19998, 
    -122.550049, 394.110107, -472.800078, -209.580049, -407.540039, 
    -417.01001, -201.519902, -388.510005, -53.469971, -122.640014, 
    -321.61001, -193.259985, -46.1800539999999, -142.599976, 
    -13.0599850000001, -79.8400389999999, 172.859985, 46.090054, 
    -148.8, -56.2900539999998, 122.75, -20.2799070000001, -113.240039, 
    87.860034, -31.580078), .Tsp = c(2000.5, 2014.75, 4), class = "ts"), 
    cpi = structure(c(-2.62, 1.16000000000003, -3.26000000000005, 
    0.220000000000027, -3.50999999999999, 0.839999999999975, 
    -2.81, -0.339999999999975, -4.56999999999999, -0.120000000000005, 
    -3.94999999999999, -1.37, -2.73000000000002, 0.350000000000023, 
    -5.38, -4.43000000000001, -3.08000000000004, 0.740000000000009, 
    -3.02999999999997, -1.09000000000003, -2, 0.350000000000023, 
    -1.51999999999998, 1.27999999999997, 0.199999999999989, -0.25, 
    -4.55000000000001, -2.48999999999995, -4.24000000000001, 
    -0.310000000000002, -2.96000000000004, -2.23999999999995, 
    -0.460000000000036, -0.0600000000000023, -2.66999999999996, 
    -1.27000000000004, -1.39999999999998, -0.699999999999989, 
    -0.960000000000036, -2.18000000000001, -2.52999999999997, 
    -0.519999999999982, -1.74000000000001, -2.18000000000001, 
    -1.40000000000003, -0.339999999999975, -0.089999999999975, 
    -1.65000000000003, -1.14999999999998, -0.170000000000016, 
    -2.00999999999999, -1.38, -1.24000000000001, 0.089999999999975, 
    -2.44, -1.91999999999996, -2.61000000000001, -0.340000000000032
    ), .Tsp = c(2000.5, 2014.75, 4), class = "ts"), constrPriceIndex = structure(c(-0.299999999999997, 
    -0.299999999999997, -1.4210854715202e-14, 0.200000000000017, 
    0.499999999999986, -0.699999999999989, 0.299999999999997, 
    -0.100000000000009, 0.300000000000011, -0.900000000000006, 
    -0.100000000000009, 0.800000000000011, -0.200000000000003, 
    -0.200000000000003, -0.299999999999997, 0.200000000000003, 
    -0.100000000000009, -0.0999999999999943, -0.16686, 0.416730000000001, 
    -0.0833399999999926, 0.166689999999988, 0.250039999999998, 
    -0.333389999999994, -0.416730000000001, -0.50009, 0.250039999999998, 
    0.833480000000009, -0.08335000000001, -0.0833399999999926, 
    -0.333400000000012, 0.333390000000009, 1.00000000031741e-05, 
    0.0833399999999926, -0.0833499999999958, 0.41673999999999, 
    0.166690000000017, -0.166690000000017, -0.135139999999993, 
    0.156170000000003, 1.00000000031741e-05, -0.468550000000008, 
    0.156190000000009, 0.54661999999999, 0, -0.234259999999992, 
    0.0780799999999857, 0.0780900000000173, 0.156179999999992, 
    -0.312359999999998, 0.0780999999999921, 0.312350000000009, 
    -0.156180000000006, -0.234269999999995, 0.0780900000000031, 
    9.99999998896328e-06, -0.0780999999999921), .Tsp = c(2000.75, 
    2014.75, 4), class = "ts"), resProp.Dwell2 = structure(c(1.70000000000002, 
    -0.400000000000006, -7.5, -1, -2.40000000000001, 0, 1.2, 
    0.700000000000003, -4.3, 0, 3.3, -18.3, 11.2, -4.90000000000001, 
    4.90000000000001, -0.300000000000011, -1.8, 2.7, 0.900000000000006, 
    -10.8, -2.59999999999999, 6.09999999999999, -0.0999999999999943, 
    -6.3, 1.2, 0.799999999999997, 4.09999999999999, -3.5, 4.60000000000001, 
    -0.200000000000003, -2.7, -15, 8, -0.0999999999999943, -0.100000000000009, 
    0.400000000000006, -4.90000000000001, 0.5, 2.7, -2.5, 1.90000000000001, 
    -4.60000000000001, -1, 8.10000000000001, -4.5, 0.299999999999997, 
    0.700000000000003, 2.19999999999999, -0.5, -3.8, 1.8, -4.69999999999999, 
    5.89999999999999, -2, 2.3, -0.399999999999991, -1.40000000000001, 
    2.3), .Tsp = c(2000.5, 2014.75, 4), class = "ts"), resProp.Dwell3 = structure(c(2.09999999999999, 
    1.29999999999998, -2.5, -4.19999999999999, 5, -2.30000000000001, 
    -1.89999999999998, -3.70000000000002, 1.20000000000002, -1.20000000000002, 
    0.0999999999999943, -9.59999999999999, -4.40000000000001, 
    -5.39999999999999, 3.2, -3.8, -1.5, 0.899999999999991, 0.700000000000003, 
    -6.7, 1.5, -2.19999999999999, 2, -2, -1.80000000000001, -2.69999999999999, 
    0.299999999999997, 2.3, 0.0999999999999943, -1.19999999999999, 
    -2.10000000000001, 1.5, -2.7, 0.700000000000003, -1.3, -0.700000000000003, 
    -2.7, 1, -1, 0.900000000000006, -4.90000000000001, 0.900000000000006, 
    -8.5, 12.7, -4.7, 1.40000000000001, 1.19999999999999, -3.39999999999999, 
    6.09999999999999, -5.39999999999999, 2.39999999999999, -1.2, 
    4, -8.09999999999999, 7.90000000000001, -9.60000000000001, 
    0.600000000000009, 2.39999999999999), .Tsp = c(2000.5, 2014.75, 
    4), class = "ts"), resProp.Dwell5 = structure(c(0.100000000000009, 
    0.899999999999991, -2.89999999999999, 2.7, 13.7, 3, 5.59999999999999, 
    -12, -0.5, -0.200000000000003, 2.5, -12, 6.7, 3.09999999999999, 
    4.7, 0.199999999999989, -3.59999999999999, -0.599999999999994, 
    1.8, -3.59999999999999, 2.59999999999999, -8.2, 3.80000000000001, 
    -4.5, 4.69999999999999, 3.40000000000001, -3.40000000000001, 
    -1.59999999999999, 1.09999999999999, -8.5, 2.40000000000001, 
    -3.8, 1.39999999999999, -7.09999999999999, 3.40000000000001, 
    -7.5, 4.3, 0.299999999999997, 1.90000000000001, -1.80000000000001, 
    0.700000000000003, -8.5, -0.700000000000003, 2.5, 2, 1.40000000000001, 
    2.7, -2.2, -2, 1.7, -2.2, 6.90000000000001, 2.69999999999999, 
    -8.8, 4.40000000000001, -5.40000000000001, 3.8, -5.69999999999999
    ), .Tsp = c(2000.5, 2014.75, 4), class = "ts"), stat.resProp.Dwell.4 = structure(c(0.000887705, 
    0.007954085, -0.025859667, 0.024097552, 0.114052787, 0.023329207, 
    0.042143181, -0.092587287, -0.004050228, -0.001624696, 0.020121403, 
    -0.100502922, 0.057354185, 0.025463388, 0.037409854, 0.001561281, 
    -0.028482938, -0.004827041, 0.014411779, -0.029034298, 0.021053409, 
    -0.067963182, 0.032070259, -0.038091783, 0.039751534, 0.027802281, 
    -0.027802281, -0.013355791, 0.009201236, -0.073403679, 0.021277398, 
    -0.033901552, 0.012624153, -0.065733979, 0.032017801, -0.072042665, 
    0.041936911, 0.002861232, 0.017933468, -0.01698154, 0.006638242, 
    -0.08375153, -0.007220248, 0.0255507, 0.019980685, 0.013752673, 
    0.026000502, -0.021134312, -0.019608471, 0.0166916, -0.021654389, 
    0.066402455, 0.024828862, -0.083302632, 0.042518482, -0.052439198, 
    0.037186281, -0.056311172, -0.012270093), .Tsp = c(2000.25, 
    2014.75, 4), class = "ts"), stat.lohn = structure(c(0, -0.00205138, 
    -0.004210738, 0, 0, -0.002848631, -0.005891227, -0.000983501, 
    0, 0.010805365, 0.022078016, 0.001188077, 0, -0.003245914, 
    -0.006556193, -0.000204576, 0, -0.005675448, -0.011619773, 
    0, 0, -0.000960638999999999, -0.001986805, 0, 0, 0.003386201, 
    0.006977449, 0, 0, -0.00183715, -0.003776571, -0.002818337, 
    0, 0.002773211, 0.005692673, 0.002818337, 0, 0.003090824, 
    0.006288291, 0, 0, -0.006644821, -0.013592439, 0, 0, 0.000586644999999998, 
    0.001211144, 0, 0, -0.001617326, -0.003344299, -0.002017983, 
    -0.007343864, 0.004398871, 0.009057395), .Tsp = c(2001.25, 
    2014.75, 4), class = "ts"), stat.resProp.Dwell.1 = structure(c(0.012777325, 
    -0.002991775, -0.057819571, -0.00796817, -0.019386714, 0, 
    0.009740337, 0.005638356, -0.035148694, 0, 0.027084134, -0.160377856, 
    0.101169235, -0.043007944, 0.043007944, -0.002580647, -0.015625318, 
    0.023347364, 0.007662873, -0.09607383, -0.024575906, 0.056733018, 
    -0.000904568, -0.058703392, 0.011450507, 0.007561473, 0.037879817, 
    -0.032246, 0.042169401, -0.001796946, -0.024580209, -0.148788737, 
    0.082097362, -0.000985707, -0.00098668, 0.003940892, -0.049380309, 
    0.005151995, 0.027371197, -0.025317808, 0.019299736, -0.047382704, 
    -0.010604553, 0.082827084, -0.04516573, 0.003075348, 0.007139245, 
    0.022111454, -0.004982571, -0.038701368, 0.018519048, -0.049096021, 
    0.061254226, -0.020346582, 0.023363175, -0.00402415, -0.014213437, 
    0.023245109, 0.027587957), .Tsp = c(2000.25, 2014.75, 4), class = "ts")), .Names = c("oenb_dependent", 
"gdp", "atx", "cpi", "constrPriceIndex", "resProp.Dwell2", "resProp.Dwell3", 
"resProp.Dwell5", "stat.resProp.Dwell.4", "stat.lohn", "stat.resProp.Dwell.1"
))

我想使用char ,作为小数分隔符将每个对象写入单独的文件。

然而,当仅针对一个ts系列进行此操作时,它可以完美地工作,但不适用于所有11。此外,在使用dec = ','写入文件时使用write.table不会将我的小数点转换为,

这是我的代码,我在所需的文件夹中只收到一个文件:

for (i in length(res) ) {
  #create data.frame
  df <- NULL
  df <- data.frame(yq = yearqtr(index(res[[i]])), val = res[[i]])
  #create path
  path <- paste("C:/Users/Desktop/Output/", names(res[i]),"-stationary.csv")
  path <- gsub(" ", "", path, fixed = TRUE)
  write.table(df, file = path,row.names=FALSE, na="", sep = ";", dec = ",")
}

感谢您的回复!

1 个答案:

答案 0 :(得分:2)

library(zoo)

for (i in seq_along(res)) {

  current <- res[[i]]

  df <- data.frame(yq = yearqtr(index(current)), 
                   val = coredata(current))
  path <- paste0("C:/Users/Desktop/Output/", names(res)[i], ".csv")
  write.table(df, file = path, 
              row.names=FALSE, na="", 
              sep = ";", dec = ",")
}

出了什么问题?

  • 在循环的开头,你没有写一个向量,只是length(res)
  • 我添加了coredata来提取时间序列中的值。
  • 使用coredata为您提供数字数据,进而允许write.table中的格式正确。
  • 我使用paste0来保存gsub的使用。