Seyma Kalay
Seyma Kalay

Reputation: 2859

How to set for loop on multiple survey questions in R

I have a survey data the for loop works when the question cmb.df$b3002 has multiple choices 1,2,3, and 4 and I can loop over each choices by table(cmb.df$b3002)[i] , but one of the questions' multiple choices were recorded as cmb.df$b3002a_1_1_mc, cmb.df$b3002a_1_2_mc,... cmb.df$b3002a_1_9_mc. How can I create a for loop on this? Many thanks in advance.

 usq <- NULL
for(i in 1:4) {
  # i-th element of `u1` squared into `i`-th position of `usq`
  usq[i] <- table(cmb.df$b3002)[i]
}

usq
[1] 5894  472  180   43

enter image description here

the dataset is huge and

 a <- cbind(cmb.df$b3002, cmb.df$b3002a_1_1_mc, cmb.df$b3002a_1_2_mc, cmb.df$b3002a_1_3_mc, cmb.df$b3002a_1_4_mc,
           cmb.df$b3002a_1_5_mc, cmb.df$b3002a_1_6_mc, cmb.df$b3002a_1_7_mc, cmb.df$b3002a_1_8_mc, cmb.df$b3002a_1_9_mc )


   dput(head(a, 20))
structure(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), .Dim = c(20L, 
10L))

 

Upvotes: 0

Views: 62

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

Maybe the following tidyverse solution can do what the question asks for. It creates a column "b300a" by reshaping from wide to long format.

library(dplyr)
library(tidyr)

cmb.df %>%
  pivot_longer(cols = starts_with("b3002a"), names_to = "b3002a") %>%
  mutate(b3002a = sub("^.*(\\d+)_mc", "\\1", b3002a),
         b3002a = as.integer(b3002a)*value) %>%
  select(-value) %>%
  na.omit()

Upvotes: 1

Related Questions