Hello, I'm a student at the university, I'm trying to learn to code all by myself. I need some help for an exercice.
For exemple the first variable "University 1" is equal to 1 in line 1, 9 and 10 and I want to mean the productivity on line 1, 9 and 10 and put this result in a table.
I would like to do this for each university variables. In my data base i have more than 50 university and 1000 observations, i have put only 10 university and 20 observations for the explanation
It would be better to provide code to generate a suitable table than to show a literal table, as that is very inconvenient to recreate in an R session. Below I create randomly a small table consisting of only 1 and 0 values.
Now to the main question. You can filter table rows like this: DF[(DF$uni1 == 1), ], and you can repeat the process for every uni by using the apply() family of functions. I chose 'vapply()' as the return of each iteration is a single value.
Here is how these three go together:
# Repeated for all columsn except the last
res <- vapply(names(DF)[1:length(DF)-1],
function(x) {
mean(DF[(DF[[x]] == 1), "prod"])
},
numeric(1) )
## uni1 uni2 uni3
## 758.000 821.000 628.875
The result is a named vector. You can then assign it to a column of a table.