将具有不同长度的数值向量列表转换为data.frame [英] Convert a list of numeric vectors with different lengths to data.frame

查看:86
本文介绍了将具有不同长度的数值向量列表转换为data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个df:

dput(head(data))

structure(list(company_code = c(1L, 1L, 1L, 1L, 1L, 11L, 11L, 
11L, 12L, 13L, 13L), company_name = c("AB Billingsfors-Långed", 
"AB Billingsfors-Långed", "AB Billingsfors-Långed", "AB Billingsfors-Långed", 
"AB Billingsfors-Långed", "AB Iggesunds B", "AB Iggesunds B", 
"AB Iggesunds B", "AB Industripapp", "AB Klippans FinpB", "AB Klippans FinpB"
), year_cg_code = c(11920L, 11920L, 11920L, 11920L, 11920L, 111929L, 
111929L, 111929L, 121929L, 131929L, 131929L), plant_code = c(122L, 
123L, 125L, 124L, 110L, 186L, 187L, 188L, 362L, 120L, 199L), 
    plant_name = c("Billingsfors", "Billingsfors", "Långed", 
    "Billingsfors", "Jåssefors", "Iggesund", "Iggesund", "Iggesund", 
    "Värö", "Fredriksberg", "Klippan"), plant_location = c("Billingsfors", 
    "Billingsfors", "Dals Långed", "Billingsfors", "Dals Långed", 
    "Iggesund", "Iggesund", "Iggesund", "Värö", "Lessebo", "Klippan"
    ), plant_location_by_municipal = c("Bengtsfors", "Bengtsfors", 
    "Bengtsfors", "Bengtsfors", "Bengtsfors", "Hudiksvall", "Hudiksvall", 
    "Hudiksvall", "Varbergs", "Lessebo", "Klippan"), year = c(1920L, 
    1920L, 1920L, 1920L, 1920L, 1929L, 1929L, 1929L, 1929L, 1929L, 
    1929L), Output_value_1000_sek = c(384L, 1920L, 1689L, 2288L, 
    6183L, 1620L, 4224L, 4248L, 459L, 1458L, 2958L), Labour_cost_1000_sek = c(50L, 
    252L, 207L, 615L, 396L, 229L, 638L, 744L, 93L, 216L, 648L
    ), Capital_cost_1000_sek = c(66L, 330L, 450L, 648L, 540L, 
    243L, 858L, 1008L, 57L, 300L, 270L), Electricity_cost_1000_sek = c(10L, 
    80L, 10L, 20L, 30L, 310L, 170L, 140L, 20L, 50L, 130L), Raw_material_cost_1000_sek = c(80L, 
    402L, 414L, 780L, 2880L, 391L, 1364L, 1416L, 345L, 354L, 
    1260L), Output_price_1_sek.ton = c(550L, 550L, 550L, 550L, 
    550L, 220L, 220L, 220L, 220L, 220L, 220L), Output_price__sek.ton = c(341L, 
    341L, 341L, 341L, 341L, 196L, 196L, 196L, 196L, 196L, 196L
    ), Labour_price_sek.hour = c(1.5625, 1.555555556, 1.710743802, 
    1.703601108, 1.571428571, 1, 1.208333333, 1.476190476, 1.081395349, 
    1.125, 1.069306931), Capital_price_interest.rate = c(6.005, 
    6.005, 6.005, 6.005, 6.005, 4.556666667, 4.556666667, 4.556666667, 
    4.556666667, 4.556666667, 4.556666667), Motive_Power_pricekr.MwH = c(22.32142857, 
    35.65062389, 28.9017341, 50.37783375, 34.36426117, 45.68228706, 
    69.64358869, 55.11811024, 72.46376812, 68.30601093, 120.3703704
    ), Electricity_price_kr.MwH = c(13.02083333, 20.83333333, 
    12.34567901, 19.34235977, 16.66666667, 25.80108198, 24.92668622, 
    24.30555556, 23.64066194, 21.92982456, 25.49019608), Raw_Material_price_kr.m3 = c(33.5, 
    33.5, 23, 27, 32, 14.5, 15.5, 11.8, 19.2, 9.8, 14.3), Mean_raw.material_price = c(27, 
    27, 27, 27, 27, 14.3, 14.3, 14.3, 14.3, 14.3, 14.3), Output_capacity_ton = c(1200L, 
    6000L, 3000L, 4700L, 9000L, 13500L, 22000L, 24000L, 1800L, 
    6000L, 6000L), Ouput_ton = c(698L, 3490L, 3070L, 4160L, 11241L, 
    7363L, 19200L, 19309L, 2086L, 6627L, 13445L), Labour_input_1000_hour = c(32L, 
    162L, 121L, 361L, 252L, 229L, 528L, 504L, 86L, 192L, 606L
    ), Capital_input_1000_sek = c(10990L, 54954L, 74937L, 108009L, 
    89925L, 6803L, 3731L, 3072L, 439L, 1097L, 2853L), Motive_Power_Mwh = c(448L, 
    2244L, 346L, 397L, 873L, 6786L, 2441L, 2540L, 276L, 732L, 
    1080L), Electric_input_Mwh = c(768L, 3840L, 810L, 1034L, 
    1800L, 12015L, 6820L, 5760L, 846L, 2280L, 5100L), Rawmaterial_input_M3 = c(2388L, 
    12000L, 18000L, 28888L, 90000L, 26965L, 88000L, 120000L, 
    17968L, 36122L, 88111L), Capacity_Utilization = c(58.16666667, 
    58.16666667, 102.3333333, 88.5106383, 124.9, 54.54074074, 
    87.27272727, 80.45416667, 115.8888889, 110.45, 224.0833333
    ), Labour_cost_share = c(13.02083333, 13.125, 12.25577265, 
    26.87937063, 6.404657933, 14.13580247, 15.10416667, 17.51412429, 
    20.26143791, 14.81481481, 21.90669371), Capital_cost_share = c(17.1875, 
    17.1875, 26.64298401, 28.32167832, 8.733624454, 15, 20.3125, 
    23.72881356, 12.41830065, 20.57613169, 9.127789047), Electricity_cost_share = c(2.604166667, 
    4.166666667, 0.592066311, 0.874125874, 0.485201359, 19.13580247, 
    4.024621212, 3.29566855, 4.357298475, 3.429355281, 4.394861393
    ), Raw_Material_cost_share = c(20.83333333, 20.9375, 24.51154529, 
    34.09090909, 46.57933042, 24.13580247, 32.29166667, 33.33333333, 
    75.16339869, 24.27983539, 42.59634888), Labour_productivity = c(0.974515229, 
    0.962484177, 1.13353828, 0.514836483, 1.992909572, 1.43648966, 
    1.624615125, 1.711640019, 1.083674961, 1.542051677, 0.991222995
    ), Capital_productivity = c(0.26451432, 0.264495066, 0.170621412, 
    0.160407564, 0.52061414, 4.50745941, 21.43339784, 26.1740221, 
    19.79351964, 25.15276216, 19.62710167), Power_productivity = c(0.857142857, 
    0.855614973, 4.88150289, 5.763224181, 7.082474227, 0.23872679, 
    1.730438345, 1.672440945, 1.663043478, 1.991803279, 2.738888889
    ), Electricity_productivity = c(0.5, 0.5, 2.085185185, 2.212765957, 
    3.435, 0.134831461, 0.619354839, 0.7375, 0.542553191, 0.639473684, 
    0.58), Raw.material.productivity = c(1.758105508, 1.74931498, 
    1.025863799, 0.866163123, 0.751253092, 1.642397254, 1.312327987, 
    0.967837334, 0.698294143, 1.103491562, 0.917813646), uniquez = c("AB Billingsfors-Långed - 122", 
    "AB Billingsfors-Långed - 123", "AB Billingsfors-Långed - 125", 
    "AB Billingsfors-Långed - 124", "AB Billingsfors-Långed - 110", 
    "AB Iggesunds B - 186", "AB Iggesunds B - 187", "AB Iggesunds B - 188", 
    "AB Industripapp - 362", "AB Klippans FinpB - 120", "AB Klippans FinpB - 199"
    )), .Names = c("company_code", "company_name", "year_cg_code", 
"plant_code", "plant_name", "plant_location", "plant_location_by_municipal", 
"year", "Output_value_1000_sek", "Labour_cost_1000_sek", "Capital_cost_1000_sek", 
"Electricity_cost_1000_sek", "Raw_material_cost_1000_sek", "Output_price_1_sek.ton", 
"Output_price__sek.ton", "Labour_price_sek.hour", "Capital_price_interest.rate", 
"Motive_Power_pricekr.MwH", "Electricity_price_kr.MwH", "Raw_Material_price_kr.m3", 
"Mean_raw.material_price", "Output_capacity_ton", "Ouput_ton", 
"Labour_input_1000_hour", "Capital_input_1000_sek", "Motive_Power_Mwh", 
"Electric_input_Mwh", "Rawmaterial_input_M3", "Capacity_Utilization", 
"Labour_cost_share", "Capital_cost_share", "Electricity_cost_share", 
"Raw_Material_cost_share", "Labour_productivity", "Capital_productivity", 
"Power_productivity", "Electricity_productivity", "Raw.material.productivity", 
"uniquez"), row.names = c(1L, 2L, 3L, 4L, 5L, 255L, 256L, 257L, 
258L, 259L, 260L), class = "data.frame")

然后我使用lapply来获得df中每年出现的library(Benchmarking)的技术效率:

I then use lapply to get the technical efficiencies with library(Benchmarking)for each year present in df:

hh=lapply(unique(data$year),function(x){

  library(Benchmarking)

  datat=data[data$year==x,]
  y <- datat[,"Output_capacity_ton",drop=FALSE]
  rownames(y)=paste(datat[,2],"-",datat[,4])

  x=with(datat,cbind(Labour_input_1000_hour,Capital_input_1000_sek,Electric_input_Mwh,Rawmaterial_input_M3))

  rownames(x)=paste(datat[,2],"-",datat[,4],"-",datat[,3])

  e <- dea(x,y)

  return(e$eff) }

  )

然后我用plyr包创建上述列表的data.frame

Then I create a data.frame of the above list with the plyr package

library(plyr)

x <- ldply(hh, data.frame)

rownames(x)=paste(data$uniquez,data$year_cg_code)

colnames(x)=c("Year",unique(data$year))

这就是我得到的:

head(x)
                                   Year      1920      1929
AB Billingsfors-Långed - 122 11920 1920 0.9854402        NA
AB Billingsfors-Långed - 123 11920 1920 0.9935262        NA
AB Billingsfors-Långed - 125 11920 1920 0.6968468        NA
AB Billingsfors-Långed - 124 11920 1920 0.8462399        NA
AB Billingsfors-Långed - 110 11920 1920 0.9620256        NA
AB Iggesunds B - 186 111929        1929        NA 0.9906348

它可以工作,但是我无法确定如何使用lapply函数来创建相同的data.frame x?有人吗?:)

It works but I can't get my mind around how I could instead use lapply function to create the same data.frame x as above? Someone?:)

推荐答案

您的lapply结果似乎是长度为1的列表,该列表是数字矢量.您可以直接使用as.data.frame获得所需的结果,然后可以设置rowcolumn名称:

Your lapply result seems to be a list of length 1 which is a numeric vector. You can directly use as.data.frame to get the result you require and then you can set the row and column names:

hh <- as.data.frame(hh)
rownames(hh) <- seq_len(nrow(hh))
colnames(hh) <- c("x")

> hh
  x
1 1
2 1
3 1
4 1
5 1
6 1

这篇关于将具有不同长度的数值向量列表转换为data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆