I'm interested in replacing height values (fu_cmi) to NA if two other variables are not equal to 1, indicating height was taken on that visit (fu_cm_takeni & fu_cm_yni) . The i indicates follow up visit number and ranges from 2 to 20. The error message I receive is
' Error in for (. in i) 2:20 : 4 arguments passed to 'for' which requires 3'
cleaned_data_wide_115 <- cleaned_data_wide_115 %>%
for (i in 2:20) {
if (fu_cm_taken[i]!=1 & fu_cm_yn[i]!=1) {
fu_cm[i] = NA
}
else {
fu_cm[i] = fu_cm[i]
}
}
The error is caused by the %>% on the first line that causes the first argument of for to be dataframe cleaned_data_wide_115 .
You could do something like the example below.
# define a test dataframe
cleaned_data_wide_115 <-
data.frame(
fu_cm_taken = rep(c(1,2,3),7),
fu_cm_yn = rep(c(2,1,4),7),
fu_cm = 1:21
)
cleaned_data_wide_115 <- within (cleaned_data_wide_115,
{
for (i in 2:20) {
if (fu_cm_taken[i]!=1 & fu_cm_yn[i]!=1) {
fu_cm[i] = NA
}
else {
fu_cm[i] = fu_cm[i] # why do you need this statement?
}
}
}
)
cleaned_data_wide_115$i <- NULL
print(cleaned_data_wide_115)
#> fu_cm_taken fu_cm_yn fu_cm
#> 1 1 2 1
#> 2 2 1 2
#> 3 3 4 NA
#> 4 1 2 4
#> 5 2 1 5
#> 6 3 4 NA
#> 7 1 2 7
#> 8 2 1 8
#> 9 3 4 NA
#> 10 1 2 10
#> 11 2 1 11
#> 12 3 4 NA
#> 13 1 2 13
#> 14 2 1 14
#> 15 3 4 NA
#> 16 1 2 16
#> 17 2 1 17
#> 18 3 4 NA
#> 19 1 2 19
#> 20 2 1 20
#> 21 3 4 21
Created on 2023-08-25 with reprex v2.0.2
cleaned_data_wide_115 <-
data.frame(
fu_cm_taken1 =1,
fu_cm_yn1=NA
fu_cm1= 70,
fu_cm_taken2=0,
fu_cm_yn2=NA,
fu_cm2=0,
fu_cm_taken3=NA,
fu_cm_yn3=0,
fu_cm3=0,
)
and so on, up until fu_cm_yn20, fu_cm_taken20, and fu_cm20. I would like to replace the height to be NA if neither fu_cm_yn[i] nor fu_cm_taken[i] are 1, which indicates a height was taken on that visit.