i have to replace some blanks in a datasets (“” characters) whit a sample on the vector that has the same column of the element and the same patient
I did a for loop like this
for(j in c(2:4,6:7,10:length(df))){
for(i in 1:length(df$PatientName)){
if(df[i,j] == ""){
df[i,j] <- sample(df[df[,1] == df[i,1] & df[,j] != "", j],1)
} else {return()}
}
}
the data.frame has this structure
df <- data.frame(Patients = rep(c("Patient_1", "Patient_2", "Patient_3"), 100), var1 = rnorm(300), var2 = rnorm(300))
New contributor
Iacopo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2