Reputation: 93
I have this data.frame. What I need to do is create 2 additional columns with the means of every other columns with the same kind of data.
Here is the dput(head())
of the file
structure(list(COLE_CODIGO_COLEGIO = c(182L, 3046L, 3327L, 3418L,
3459L, 3525L), Utilidad12.1 = c(5.67960793875611e+84, 6.70878394856429e+83,
3.10783852265787e+84, 1.80000434506295e+84, 7.92256239238555e+84,
2.6032583906869e+83), genero.x = c(0.581395348837209, 0.525423728813559,
0.636363636363636, 0.55, 0.488636363636364, 0.63768115942029),
Utilidad11.1 = c(1.15336934758534e+73, 3.77916671655328e+72,
4.7319512062371e+73, 7.97038534283783e+72, 1.32237182934735e+73,
6.55595718179632e+72), genero.y = c(0.527472527472527, 0.616666666666667,
0.526315789473684, 0.560975609756098, 0.450704225352113,
0.742268041237113), Utilidad10.1 = c(5.09613856124168e+72,
5.25727948275145e+71, 1.4350276514895e+72, 7.04968791434072e+72,
5.97923875650689e+72, 4.30547735977066e+72), genero.x = c(0.566371681415929,
0.315789473684211, 0.571428571428571, 0.578947368421053,
0.353658536585366, 0.743243243243243), Utilidad07.1 = c(1.401355333064e+71,
1.35282220680438e+71, 9.87359187347488e+71, 1.89236591312778e+72,
1.18195124980311e+73, 5.99279404969151e+70), genero.y = c(0.534883720930233,
0.522727272727273, 0.5, 0.560975609756098, 0.411764705882353,
0.714285714285714), Utilidad06.1 = c(4.47939696971958e+72,
2.91946645871643e+72, 3.16785158272574e+72, 1.16889985482301e+74,
5.83958109398712e+74, 5.37640536403147e+71), genero = c(0.5,
0.6, 0.357142857142857, 0.5, 0.422680412371134, 0.695652173913043
)), .Names = c("COLE_CODIGO_COLEGIO", "Utilidad12.1", "genero.x",
"Utilidad11.1", "genero.y", "Utilidad10.1", "genero.x", "Utilidad07.1",
"genero.y", "Utilidad06.1", "genero"), row.names = c(NA, 6L), class = "data.frame")
What I mean is I want to create a column with the name "MeanUtilidad" that is the mean of every other column with the name "Utilidad" in it. And a column with the name "Meangenero" that contains the mean for every other variable with the name "genera" in it.
I hope I have expressed my problem crearly enough.
I tried using the following code Semestre1["UProm"]<-apply(Semestre1 [ ,2:dim(Semestre1)[2]],1,mean, na.rm=T)
But it's taking into the mean every column and I can't figure a way to select every specific column.
Upvotes: 2
Views: 187
Reputation: 115505
rowMeans
will coerce the input to a matrix. You could use Reduce(`+`, list)
eg
nUtilidad <- grep("Utilidad", names(dat), value = TRUE)
nGenero <- grep("genero", names(dat), value = TRUE)
dat$MeanUtilidad <- Reduce(`+`, dat[nUtilidad]) / length(nUtilidad)
dat$MeanGenero <- Reduce(`+`, dat[nGenero]) / length(nGenero)
benchmarking against rowMeans for this small example
library(microbenchmark)
microbenchmark(Reduce(`+`,dat[nUtilidad])/length(nUtilidad), rowMeans(dat[nUtilidad]))
## Unit: microseconds
## expr min lq median uq max neval
## Reduce(`+`, dat[nUtilidad])/length(nUtilidad) 75.561 77.6105 79.2025 80.4055 451.967 100
## rowMeans(dat[nUtilidad]) 178.477 179.9305 180.9955 182.6255 321.482 100
Upvotes: 1
Reputation: 89097
You can do like this:
dat$MeanUtilidad <- rowMeans(dat[grep("Utilidad", names(dat), value = TRUE)])
dat$MeanGenero <- rowMeans(dat[grep("genero", names(dat), value = TRUE)])
Upvotes: 4