0

grateful for your help. I have a set of binary variables (values 0/1), one variable, say, varA, and then about 35 variables: var55, var56, var57, var58,etc up to var90 in a dataframe I called "di" and I want do some computations over these 35 variables, such as

sens55 <-sum(di$var55==1 & di$varA==1) / sum(di$varA==1) #sensitivity

How do I do a for/loop over my 35 variables (keeping varA the same), with the name of the resulting calculations as sens55, sens56, sens57, etc up to sens90 (that is, keeping the last two digits the same as the final two digits of my variable names)? Thank you.

  • 2
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's almost always better to keep related values in R in a named list rather than creating a bunch of variables with indexes in their name in your global environment. When data is in a list, it's much easier to loop/apply functions over. – MrFlick Sep 07 '22 at 16:48

1 Answers1

0

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.

Archeologist
  • 169
  • 1
  • 11