Using If / Else in R

3

This is my data frame:

tsdata=structure(list(S.Educ = c(228.3000255, 237.2500067, 248.2500235, 
235.8500022, 243.3000045, 254.9999758, 258.1000174, 261.1000144, 
268.3000056, 277.1000373, 249.2000057, 234.7000229, 239.8500266, 
0.66159, 0.67453, 0.64213, 0.6248, 0.65231, 228.3000255, 237.2500067, 
248.2500235, 235.8500022, 243.3000045, 254.9999758, 258.1000174, 
261.1000144, 268.3000056, 277.1000373, 249.2000057, 234.7000229, 
239.8500266, 0.76481, 0.81037, 0.82068, 0.83542, 0.86355, 0.88456, 
0.92678, 0.80906, 228.3000255, 237.2500067, 248.2500235, 235.8500022, 
243.3000045, 254.9999758, 258.1000174, 261.1000144, 268.3000056
), i.Educ.A = c(6.76, 6.53, 6.93, 6.99, 7.16, 7.46, 7.16, 7.49, 
7.43, 7.06, 7.43, 6.96, 6.68, 11.36, 10.86, 10.31, 10.46, 9.76, 
6.76, 6.53, 6.93, 6.99, 7.16, 7.46, 7.16, 7.49, 7.43, 7.06, 7.43, 
6.96, 6.68, 10.88, 10.81, 10.63, 9.75, 10.13, 12.44, 13.88, 13.25, 
6.76, 6.53, 6.93, 6.99, 7.16, 7.46, 7.16, 7.49, 7.43), i.Globi.A = c(14.76, 
15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 
11.01, 9.36, 9.44, 8.86, 9.69, 9.06, 9.56, 9.81, 14.76, 15.16, 
15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 11.01, 
9.36, 9.44, 12, 11.31, 10.06, 9.19, 8.72, 8.54, 9.56, 9.13, 14.76, 
15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56), i.Educ.B = c(6.76, 
6.53, 6.93, 6.99, 7.16, 7.46, 7.16, 7.49, 7.43, 7.06, 7.43, 6.96, 
6.68, 11.36, 10.86, 10.31, 10.46, 9.76, 6.76, 6.53, 6.93, 6.99, 
7.16, 7.46, 7.16, 7.49, 7.43, 7.06, 7.43, 6.96, 6.68, 10.88, 
10.81, 10.63, 9.75, 10.13, 12.44, 13.88, 13.25, 6.76, 6.53, 6.93, 
6.99, 7.16, 7.46, 7.16, 7.49, 7.43), i.Globi.B = c(14.76, 15.16, 
15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 11.01, 
9.36, 9.44, 8.86, 9.69, 9.06, 9.56, 9.81, 14.76, 15.16, 15.56, 
14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 11.01, 9.36, 
9.44, 12, 11.31, 10.06, 9.19, 8.72, 8.54, 9.56, 9.13, 14.76, 
15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56), S.Torto = c(228.3000255, 
237.2500067, 248.2500235, 235.8500022, 243.3000045, 254.9999758, 
258.1000174, 261.1000144, 268.3000056, 277.1000373, 249.2000057, 
234.7000229, 239.8500266, 237.850026, 239.0000239, 238.3000059, 
238.8500041, 239.280016, 241.7499798, 246.4500108, 235.6499768, 
234.0500179, 232.5000145, 231.6999874, 234.6700059, 233.4299733, 
224.7500106, 227.0000177, 231.5800102, 237.3000006, 245.2999909, 
241.8999792, 246.8999855, 245.7000042, 247.5000025, 251.5999872, 
254.7999858, 259.4500127, 250.9999839, 251.5000089, 251.1999823, 
248.3999935, 236.4499965, 239.0000239, 216.4999833, 211.5499951, 
202.0999805, 200.2499921), i.Torto.A = c(6.76, 6.53, 6.93, 6.99, 
7.16, 7.46, 7.16, 7.49, 7.43, 7.06, 7.43, 6.96, 6.68, 6.81, 6.56, 
6.31, 6.81, 6.56, 6.66, 6.76, 6.75, 6.37, 6.9, 6.37, 6.4, 6.5, 
6.5, 6.5, 6.5, 6.3, 6.31, 6.31, 6.38, 6.44, 6.44, 6.31, 6.38, 
6.5, 6.44, 6.38, 6.38, 6.38, 6.38, 6.44, 6.56, 7.81, 7.94, 7.5
), i.Globi.A = c(14.76, 15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 
11.56, 11.56, 10.11, 11.01, 9.36, 9.44, 8.86, 9.69, 9.06, 9.56, 
9.81, 10.36, 10.36, 9.6, 9.62, 9.9, 9.87, 9.75, 9.13, 9.13, 11.75, 
11.75, 11.6, 11.94, 12, 11.31, 10.06, 9.19, 8.72, 8.54, 9.56, 
9.13, 8.75, 7.88, 7.88, 8.31, 8.06, 8.19, 8.06, 8.19, 8.06), 
    i.Torto.B = c(6.76, 6.53, 6.93, 6.99, 7.16, 7.46, 7.16, 7.49, 
    7.43, 7.06, 7.43, 6.96, 6.68, 6.81, 6.56, 6.31, 6.81, 6.56, 
    6.66, 6.76, 6.75, 6.37, 6.9, 6.37, 6.4, 6.5, 6.5, 6.5, 6.5, 
    6.3, 6.31, 6.31, 6.38, 6.44, 6.44, 6.31, 6.38, 6.5, 6.44, 
    6.38, 6.38, 6.38, 6.38, 6.44, 6.56, 7.81, 7.94, 7.5), i.Globi.B = c(14.76, 
    15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 
    11.01, 9.36, 9.44, 8.86, 9.69, 9.06, 9.56, 9.81, 10.36, 10.36, 
    9.6, 9.62, 9.9, 9.87, 9.75, 9.13, 9.13, 11.75, 11.75, 11.6, 
    11.94, 12, 11.31, 10.06, 9.19, 8.72, 8.54, 9.56, 9.13, 8.75, 
    7.88, 7.88, 8.31, 8.06, 8.19, 8.06, 8.19, 8.06), S.Pub = c(7.6025, 
    7.9875, 8.228, 7.96, 7.99, 8.51, 8.57, 8.715, 8.8475, 9.01, 
    8.67, 8.38, 8.655, 8.67, 234.7000229, 239.8500266, 237.850026, 
    239.0000239, 238.3000059, 238.8500041, 239.280016, 241.7499798, 
    246.4500108, 235.6499768, 234.0500179, 232.5000145, 231.6999874, 
    9.971, 10.049, 10.2025, 10.62, 10.5125, 11.1175, 10.9685, 
    11.186, 11.285, 11.2925, 11.96, 234.7000229, 239.8500266, 
    237.850026, 239.0000239, 238.3000059, 238.8500041, 239.280016, 
    241.7499798, 246.4500108, 235.6499768), i.Pub.A = c(15.56, 
    15.66, 16.46, 20.06, 19.66, 15.46, 14.06, 14.06, 20.06, 21.31, 
    22.31, 19.66, 18.66, 17.16, 6.96, 6.68, 6.81, 6.56, 6.31, 
    6.81, 6.56, 6.66, 6.76, 6.75, 6.37, 6.9, 6.37, 10.75, 10.75, 
    11.25, 11.62, 11.88, 11.5, 11.88, 12, 12, 12, 11.25, 6.96, 
    6.68, 6.81, 6.56, 6.31, 6.81, 6.56, 6.66, 6.76, 6.75), i.Globi.A = c(14.76, 
    15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 
    11.01, 9.36, 9.44, 8.86, 9.36, 9.44, 8.86, 9.69, 9.06, 9.56, 
    9.81, 10.36, 10.36, 9.6, 9.62, 9.9, 9.87, 11.75, 11.75, 11.6, 
    11.94, 12, 11.31, 10.06, 9.19, 8.72, 8.54, 9.56, 9.36, 9.44, 
    8.86, 9.69, 9.06, 9.56, 9.81, 10.36, 10.36, 9.6), i.Pub.B = c(15.56, 
    15.66, 16.46, 20.06, 19.66, 15.46, 14.06, 14.06, 20.06, 21.31, 
    22.31, 19.66, 18.66, 17.16, 6.96, 6.68, 6.81, 6.56, 6.31, 
    6.81, 6.56, 6.66, 6.76, 6.75, 6.37, 6.9, 6.37, 10.75, 10.75, 
    11.25, 11.62, 11.88, 11.5, 11.88, 12, 12, 12, 11.25, 6.96, 
    6.68, 6.81, 6.56, 6.31, 6.81, 6.56, 6.66, 6.76, 6.75), i.Globi.B = c(14.76, 
    15.16, 15.56, 14.96, 14.46, 15.96, 13.16, 11.56, 11.56, 10.11, 
    11.01, 9.36, 9.44, 8.86, 9.36, 9.44, 8.86, 9.69, 9.06, 9.56, 
    9.81, 10.36, 10.36, 9.6, 9.62, 9.9, 9.87, 11.75, 11.75, 11.6, 
    11.94, 12, 11.31, 10.06, 9.19, 8.72, 8.54, 9.56, 9.36, 9.44, 
    8.86, 9.69, 9.06, 9.56, 9.81, 10.36, 10.36, 9.6), S.Hemot = c(228.3000255, 
    237.2500067, 248.2500235, 235.8500022, 243.3000045, 254.9999758, 
    258.1000174, 261.1000144, 268.3000056, 277.1000373, 249.2000057, 
    234.7000229, 239.8500266, 237.850026, 239.0000239, 238.3000059, 
    238.8500041, 239.280016, 241.7499798, 246.4500108, 235.6499768, 
    234.0500179, 232.5000145, 231.6999874, 234.6700059, 233.4299733, 
    224.7500106, 227.0000177, 231.5800102, 237.3000006, 245.2999909, 
    241.8999792, 246.8999855, 245.7000042, 247.5000025, 251.5999872, 
    254.7999858, 259.4500127, 250.9999839, 251.5000089, 251.1999823, 
    248.3999935, 236.4499965, 239.0000239, 216.4999833, 211.5499951, 
    202.0999805, 200.2499921), i.Hemot.A = c(13.8595, 14.069, 
    15.1918, 16.7617, 234.7000229, 239.8500266, 237.850026, 239.0000239, 
    238.3000059, 238.8500041, 239.280016, 241.7499798, 246.4500108, 
    235.6499768, 234.0500179, 232.5000145, 231.6999874, 13.3219, 
    13.2862, 13.19, 12.9323, 13.2048, 13.1795, 13.0952, 13.0033, 
    12.9988, 12.9988, 12.8194, 12.8194, 12.65, 12.476, 12.88, 
    13.3279, 13.36, 13.2716, 13.1788, 12.8091, 12.49, 12.733, 
    12.7595, 12.668, 12.6874, 12.6965, 12.6845, 12.6471, 12.4939, 
    12.5433, 12.555), i.Globi.A = c(14.76, 15.16, 15.56, 14.96, 
    6.96, 6.68, 6.81, 6.56, 6.31, 6.81, 6.56, 6.66, 6.76, 6.75, 
    6.37, 6.9, 6.37, 9.81, 10.36, 10.36, 9.6, 9.62, 9.9, 9.87, 
    9.75, 9.13, 9.13, 11.75, 11.75, 11.6, 11.94, 12, 11.31, 10.06, 
    9.19, 8.72, 8.54, 9.56, 9.13, 8.75, 7.88, 7.88, 8.31, 8.06, 
    8.19, 8.06, 8.19, 8.06), i.Hemot.B = c(13.8595, 14.069, 15.1918, 
    16.7617, 234.7000229, 239.8500266, 237.850026, 239.0000239, 
    238.3000059, 238.8500041, 239.280016, 241.7499798, 246.4500108, 
    235.6499768, 234.0500179, 232.5000145, 231.6999874, 13.3219, 
    13.2862, 13.19, 12.9323, 13.2048, 13.1795, 13.0952, 13.0033, 
    12.9988, 12.9988, 12.8194, 12.8194, 12.65, 12.476, 12.88, 
    13.3279, 13.36, 13.2716, 13.1788, 12.8091, 12.49, 12.733, 
    12.7595, 12.668, 12.6874, 12.6965, 12.6845, 12.6471, 12.4939, 
    12.5433, 12.555), i.Globi.B = c(14.76, 15.16, 15.56, 14.96, 
    6.96, 6.68, 6.81, 6.56, 6.31, 6.81, 6.56, 6.66, 6.76, 6.75, 
    6.37, 6.9, 6.37, 9.81, 10.36, 10.36, 9.6, 9.62, 9.9, 9.87, 
    9.75, 9.13, 9.13, 11.75, 11.75, 11.6, 11.94, 12, 11.31, 10.06, 
    9.19, 8.72, 8.54, 9.56, 9.13, 8.75, 7.88, 7.88, 8.31, 8.06, 
    8.19, 8.06, 8.19, 8.06)), .Names = c("S.Educ", "i.Educ.A", 
"i.Globi.A", "i.Educ.B", "i.Globi.B", "S.Torto", "i.Torto.A", 
"i.Globi.A", "i.Torto.B", "i.Globi.B", "S.Pub", "i.Pub.A", "i.Globi.A", 
"i.Pub.B", "i.Globi.B", "S.Hemot", "i.Hemot.A", "i.Globi.A", 
"i.Hemot.B", "i.Globi.B"), row.names = c(NA, -48L), class = "data.frame")

In this dataframe there are 4 countries: c("Educ","Torto","Pub","Hemot")

For each country my idea is to run a regression (MQO) and pull coefficients, errors, standard, pvalue, and so on. For country Educ :

Reg.Educ.A = lm (data.country $ S.Educ ~ data.country $ i.Educ.A - data.country $ i.Globi.A)
Reg.Educ.B = lm (data.country $ S.Educ ~ data.country $ i.Educ.B - data.country $ i.Globi.B)

I have developed this code below. I would like to know if I could wipe this code more, perhaps using else ?

   country <- c("Educ", "Torto", "Pub", "Hemot")

    coef.matrix=matrix(0,4,4)

for (i in 1:4){

  if (country[i]=="Educ"){
    data.country=tsdata[,1:5]
    coef.matrix[1,1]=summary(lm(data.country$S.Educ~data.country$i.Educ.A - data.country$i.Globi.A))$coef[1,1]
    coef.matrix[1,2]=summary(lm(data.country$S.Educ~data.country$i.Educ.A - data.country$i.Globi.A))$coef[2,1]

    coef.matrix[1,3]=summary(lm(data.country$S.Educ~data.country$i.Educ.B - data.country$i.Globi.B))$coef[1,1]
    coef.matrix[1,4]=summary(lm(data.country$S.Educ~data.country$i.Educ.B - data.country$i.Globi.B))$coef[2,1]

  }
  if (country[i]=="Torto"){
    data.country=tsdata[,6:10]
    coef.matrix[2,1]=summary(lm(data.country$S.Torto~data.country$i.Torto.A - data.country$i.Globi.A))$coef[1,1]
    coef.matrix[2,2]=summary(lm(data.country$S.Torto~data.country$i.Torto.A - data.country$i.Globi.A))$coef[2,1]

    coef.matrix[2,3]=summary(lm(data.country$S.Torto~data.country$i.Torto.B - data.country$i.Globi.B))$coef[1,1]
    coef.matrix[2,4]=summary(lm(data.country$S.Torto~data.country$i.Torto.B - data.country$i.Globi.B))$coef[2,1]
  }
  if (country[i]=="Pub"){
    data.country=tsdata[,11:15]
    coef.matrix[3,1]=summary(lm(data.country$S.Pub~data.country$i.Pub.A - data.country$i.Globi.A))$coef[1,1]
    coef.matrix[3,2]=summary(lm(data.country$S.Pub~data.country$i.Pub.A - data.country$i.Globi.A))$coef[2,1]

    coef.matrix[3,3]=summary(lm(data.country$S.Pub~data.country$i.Pub.B - data.country$i.Globi.B))$coef[1,1]
    coef.matrix[3,4]=summary(lm(data.country$S.Pub~data.country$i.Pub.B - data.country$i.Globi.B))$coef[2,1]

  }
  if (country[i]=="Hemot"){
    data.country=tsdata[,16:20]
    coef.matrix[4,1]=summary(lm(data.country$S.Hemot~data.country$i.Hemot.A - data.country$i.Globi.A))$coef[1,1]
    coef.matrix[4,2]=summary(lm(data.country$S.Hemot~data.country$i.Hemot.A - data.country$i.Globi.A))$coef[2,1]

    coef.matrix[4,3]=summary(lm(data.country$S.Hemot~data.country$i.Hemot.B - data.country$i.Globi.B))$coef[1,1]
    coef.matrix[4,4]=summary(lm(data.country$S.Hemot~data.country$i.Hemot.B - data.country$i.Globi.B))$coef[2,1]
  }
}
    
asked by anonymous 04.04.2017 / 01:21

1 answer

1

The first big question is to organize your data in the right way. I recommend reading the tidydata related materials in the R ( here and here ).

There are columns with repeated names in your database, I did not consider this in the answer, and arbitrarily took the first column when there was duplicity.

Packages used

library(tidyverse)
library(stringr)
library(broom)

Starting the data organization to a suitable format

ts_tidy <- tsdata[ , unique(names(tsdata))] %>% 
      as_tibble() %>% 
      gather() %>% 
      separate(key, c("v1", "pais", "grupo")) %>%
      group_by(pais, v1, grupo) %>%
      nest()

aux_globi <- ts_tidy %>% 
  filter(pais == "Globi") %>% 
  select(grupo, Globi = data)

aux_s <- ts_tidy %>% 
  filter(v1 == "S") %>% 
  select(pais, S = data)

aux_i <- ts_tidy %>% 
  filter(v1 == "i") %>% 
  select(pais, grupo, i = data)

Organized database

ts <- aux_s %>% 
  left_join(aux_i, by = "pais") %>% 
  left_join(aux_globi, by = "grupo") %>% 
  unnest() %>% 
  'names<-'(c("pais", "grupo", "S", "i", "Globi"))
> ts
# A tibble: 384 × 5
    pais grupo      S     i Globi
   <chr> <chr>  <dbl> <dbl> <dbl>
1   Educ     A 228.30  6.76 14.76
2   Educ     A 237.25  6.53 15.16
3   Educ     A 248.25  6.93 15.56
4   Educ     A 235.85  6.99 14.96
5   Educ     A 243.30  7.16 14.46
6   Educ     A 255.00  7.46 15.96
7   Educ     A 258.10  7.16 13.16
8   Educ     A 261.10  7.49 11.56
9   Educ     A 268.30  7.43 11.56
10  Educ     A 277.10  7.06 10.11
# ... with 374 more rows

Adjusting Your Templates

mod <- ts %>% 
  group_by(pais, grupo) %>% 
  nest() %>% 
  mutate(
    ajuste = data %>% map(~ lm(S ~ i - Globi, data = .)),
    resumo = map(ajuste, glance),
    coef = map(ajuste, tidy),
    resid = map(ajuste, augment)
  )

Access to adjustment statistics

mod %>% unnest(resumo)
mod %>% unnest(coef)
mod %>% unnest(resid)
    
05.04.2017 / 14:22