There are much more efficient ways to loop through data frames and lists in R comparing to a for
loop. One good practice is that if you are calculating a measure for each row in a data frame, then using dplyr::mutate
would be the best option. I came up with the data frame based on information you had provided in your question, so my resultant measure may not be very accurate, but I hope this helps:
#packages:
library(dplyr)
#I suppose your data frame might look like this based on your question:
DI <- data.frame(55:90, rep(c(0,1)), paste("var", 55:90, sep = ""), rep(c(0,1)))
names(DI) <- c("Index", "VarA", "Variables", "B_Signals")
head(DI)
- The reason I add a column
55:90
under the name Index
is to be able to use it in creating the variable names sens##
like sens55
in your example above.
paste
is to concatenate the strings to create the variable names var55
, var56
, etc.
First six rows of the data frame DI
:
Index VarA Variables B_Signals
1 55 0 var55 0
2 56 1 var56 1
3 57 0 var57 0
4 58 1 var58 1
5 59 0 var59 0
6 60 1 var60 1
The functions below create two columns; one for sensitivity
measure using your calculation above and the other to create the sens##
variable name:
NewDI <- DI %>%
mutate(Sensitivity = sum(Variables == 1 & VarA == 1) / sum(VarA == 1)) %>%
mutate(Sensitivity_Var_name = paste("sens", Index, sep = ""))
head(NewDI)
First six rows of the data frame NewDI
:
Index VarA Variables B_Signals Sensitivity Sensitivity_Var_name
1 55 0 var55 0 0 sens55
2 56 1 var56 1 0 sens56
3 57 0 var57 0 0 sens57
4 58 1 var58 1 0 sens58
5 59 0 var59 0 0 sens59
6 60 1 var60 1 0 sens60
Lastly, I created a new data frame from NewDI
named SensitivityDF
to list only the sensitivity variable name and the sensitivity measure:
SensitivityDF <- NewDI %>%
select(Sensitivity_Var_name, Sensitivity)
head(SensitivityDF)
First six rows of the data frame SensitivityDF
:
Sensitivity_Var_name Sensitivity
1 sens55 0
2 sens56 0
3 sens57 0
4 sens58 0
5 sens59 0
6 sens60 0
The last data frame SensitivityDF
allows you a quick access for each sensitivity name and its corresponding measure as needed.
Please note that the Sensitivity
column is zeros because there is not enough information on what you are trying to do with your calculation or how your data might look like. Also note that if you have those variables in scattered lists/vectors, I suggest creating a data frame of those lists/vectors and use it for processing, creating measures, etc.