使用R中的IF / Else语句

时间:2017-04-03 20:09:23

标签: r

这是我的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")

我有4个国家:c("Educ","Torto","Pub","Hemot")

对于每个国家,我需要进行回归。例如,对于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)

我不是很擅长R.我要做的就是这个,但它没有用。

我需要使用If/else语句。

这个想法是:“如果国家是”Educ“,我将使用data.countra = tsdata [,1:5]。如果国家是”Torto“,我将使用data.country = tsdata [,6 :10],依此类推,直到第四个国家为“Hemot”。

以下代码是我能做的最大值,这是错误的。我不知道如何使用它“if / 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]
  }
}

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

如果您使用长格式数据,这会更简单。例如,使用data.table ...

# fix data
library(data.table)
DT = melt(setDT(tsdata), 
  meas = split(seq_along(tsdata), 1:5), 
  value.name = c("S", "iA", "iGA", "iB", "iGB"),
  variable.name = "country"
)

# run models, extracting coefficients    
DT[, c(
  A = as.list(coef(lm(S ~ iA - iGA))),
  B = as.list(coef(lm(S ~ iB - iGB)))
), by=country]


   country A.(Intercept)         A.iA B.(Intercept)         B.iB
1:       1      613.7994 -52.85050685      613.7994 -52.85050685
2:       2      277.2805  -5.47557795      277.2805  -5.47557795
3:       3      333.4474 -19.39535189      333.4474 -19.39535189
4:       4      236.4338   0.05428531      236.4338   0.05428531