0
votes

I am really new in Stack community and also with R. I am trying to get three specific values after bunch of codes for one year i.e 2000.So, I get one row with three columns. However, I need to get those specific values as output for 14 more years, until 2014.How can I loop in a non-messy way, need some suggestion. I am trying following codes:

CVA <-data.frame()
for(i in 2000:2014) {
  
  
D.2000 <-  filter(Deu.niot.new, ...1==i)
D.dom.2000 <-filter(Deu.2000,...4=="Domestic")

D.imp.2000 <-filter(Deu.2000,...4=="Imports")


D.ndom.2000 <-select(Deu.2000.dom,-(...61:...67))
D.nimp.2000 <-select(Deu.2000.imp,-(...61:...67)) 

and so on....(MORE CODES)

then, I need the below one as my final result:

CVA [[i,]]<-cbind(cva.agri.2000,cva.manu.2000,cva.ser.2000)

Which means I need to create 14 more rows with 3 columns using for loops. Using above bunch of codes I just got the values for 2000. I need output as data.frame.

Any hint or suggestion would be helpful!

I used dput to show my data structure, seems really messy though:

structure(list(...1 = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000), ...2 = c("A01", "A02", "A03", "B", "C10-C12", "C13-C15", "C16", "C17", "C18", "C19"), ...3 = c("Crop and animal production, hunting and related service activities", "Forestry and logging", "Fishing and aquaculture", "Mining and quarrying", "Manufacture of food products, beverages and tobacco products", "Manufacture of textiles, wearing apparel and leather products", "Manufacture of wood and of products of wood and cork, except furniture; manufacture of articles of straw and plaiting materials", "Manufacture of paper and paper products", "Printing and reproduction of recorded media", "Manufacture of coke and refined petroleum products"), ...4 = c("Domestic", "Domestic", "Domestic", "Domestic", "Domestic", "Domestic", "Domestic", "Domestic", "Domestic", "Domestic"), ...5 = c(1180.79357309081, 38.9191594938878, 0.0105591974138373, 20.3813413720862, 2717.28244274957, 26.4883571262503, 47.5892951388691, 22.4047547307701, 31.3319695906172, 444.595589525748), ...6 = c(82.985934075375, 417.382557034188, 0, 0.353736300347672, 1.95091900568706, 1.19359373503498, 68.3727666976661, 2.95275151914329, 4.46816919858237, 12.3444402345904), ...7 = c(0.0148890463411341, 0.0168962782166389, 4.49517538484516, 0.030235623520195, 0.378606553315736, 9.08182032551762, 1.07007663240345, 1.26467070570843, 0.905244246509581, 2.4453813655119), ...8 = c(12.4358144539012, 23.2895865413182, 0.0166626654844983, 76.2371092458133, 21.1129192358641, 2.88107063157295, 47.303659680883, 68.154120909302, 33.2904033042654, 56.5944153631193 ), ...9 = c(17952.563202823, 5.2593139999212, 22.4131588651991, 116.814290718508, 14706.3043351145, 63.77989250888, 149.697280644864, 1269.36851882589, 239.087006514138, 345.705681202278), ...10 = c(52.8680776105079, 0.980236672662672, 0.00396924705354, 18.0442003449513, 81.890380752962, 1623.56216722411, 28.8605439638297, 193.373586146434, 206.014971131385, 61.8673029946462)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

Only 10 rows are visible here so data for 2000 only. However the data frame consists of 15 years (2000 to 2014).

Output should look like: |year |Agri |Manu | Ser| |:---:|:---:|:---:|:--:| |2000|value|value|value|

|2001|value|value|value|

So, I need to store output of three values each year as final output. So, 15 rows with three columns.

1
Hi, it is helpful if you provide a minimal working examples (stackoverflow.com/help/minimal-reproducible-example). What does your input data look likeGeorge
For loops are near extinct in R these days. It's almost always faster to change the data into a suitable form for vectorization, then restore the data to its original form. It's faster to code and faster to run.Brad
many thanks! trying to do so.Archer_Jazz
Hi, I already provided my data structure and expected output. Looking forward to further suggestion if possible.Archer_Jazz

1 Answers

0
votes

Do you have column names like ...1, ...2 ? There might be some issues importing the data.

Nonetheless, you can use the split-apply-combine approach.

do.call(rbind, lapply(split(Deu.niot.new, Deu.niot.new$`...1`), function(x) {
  #x is the dataframe for each year
  #So this step (D.2000 <-  filter(Deu.niot.new, ...1==i)) is replaced with `x`.
  D.dom.2000 <-filter(Deu.2000,...4=="Domestic")
  D.imp.2000 <-filter(Deu.2000,...4=="Imports")
  #...
  #...
  D.ndom.2000 <-select(Deu.2000.dom,-(...61:...67))
  D.nimp.2000 <-select(Deu.2000.imp,-(...61:...67)) 
  #...
  #...
  cbind(cva.agri.2000,cva.manu.2000,cva.ser.2000)
})) -> CVA

CVA