Duck
Duck

Reputation: 39595

How to create formulas to compute new variables in R considering names in a data frame and their column value

Hi everybody I am trying to solve a little problem in R to compute new variables in R. The dput version of my data frame is:

structure(list(sexo = c(-22.84754, -30.95001, -37.36658, -45.64382, 
-54.9466, -0.1732915), cli_edad = c(5.972972, 11.67697, 16.46362, 
26.57938, 47.19307, 0.1037254), edad2 = c(-0.0637181, -0.1199798, 
-0.1600652, -0.2424397, -0.4273092, -0.001068), veces_mora_ago12 = c(-100.6952, 
-166.6598, -391.6087, -710.2349, -1098.773, -0.3525356), veces_mora_sep12 = c(20.06456, 
162.5816, 388.1126, 738.8196, 1181.483, 0.2907068), veces_mora_oct12 = c(79.44273, 
-15.99917, -13.33856, 9.459844, 103.7592, -0.0863719)), .Names = c("sexo", 
"cli_edad", "edad2", "veces_mora_ago12", "veces_mora_sep12", 
"veces_mora_oct12"), class = "data.frame", row.names = c(NA, 
6L))

The rows of that data frame are the coefficients of different models. When I load a data frame in R I need to compute a new variable for each model. For example the loaded data frame DF will have the same names that z but in this case I have to compute 6 additional variables for DF this variables are defined for different formulas, for example this considering names of z and the first row of z:

DF$I1=-22.8475400*DF$sexo+5.9729720*DF$cli_edad-0.0637181*DF$edad2-100.6952000*DF$veces_mora_ago12+20.0645600*DF$veces_mora_sep12+79.4427300*DF$veces_mora_oct12

Like last formula I have to write 5 additional formulas for the combination between names and second row of z until names and sixth row of z.

I don't know if it is possible to create in R this formulas and when I load a data frame can apply these to compute the new variables. Thanks for your help.

Upvotes: 1

Views: 427

Answers (1)

Stephen Henderson
Stephen Henderson

Reputation: 6522

Your example:

> -22.8475400*DF$sexo+5.9729720*DF$cli_edad-0.0637181*DF$edad2-100.6952000*DF$veces_mora_ago12+20.0645600*DF$veces_mora_sep12+79.4427300*DF$veces_mora_oct12
[1]  17410.94776  19549.83787  47112.85467  88294.47367 144127.32240     39.04883

As @JJLagrange says I think you want this???

DM=as.matrix(DF)
DM%*% t(DM)
             1           2           3            4            5           6
1  17410.94776  19549.8379  47112.8547   88294.4737  144127.3224  39.0488287
2  19549.83787  55558.5082 129927.5614  240057.8018  375800.3450 113.9736682
3  47112.85467 129927.5614 305834.0191  566896.3661  890283.7105 260.2182340
4  88294.47367 240057.8018 566896.3661 1053167.3837 1658033.7139 475.0128054
5 144127.32240 375800.3450 890283.7105 1658033.7139 2619216.6537 736.2772169
6     39.04883    113.9737    260.2182     475.0128     736.2772   0.2570419

Upvotes: 1

Related Questions