I am using grep and grepl to search through a character variable and create simplified levels.
I have tried to get the results in a dataframe. I have also tried using if and else if statements and just designating the variables. I have attached this code and the for if statement does not run.
for(i in 1:length(D$ID)){
if(grepl("Bachelor", D$NDEGREE)[i]){D$NDegree[i] <- "Bachelors"}
else if(grepl("BS", D$NDEGREE)[i]){D$NDegree[i] <- "Bachelors"}
else if (grepl("Master", D$NDEGREE)[i]){D$NDegree[i] <- "Masters"}
else if(grepl("Doctor", D$NDEGREE)[i]){D$NDegree[i] <- "Doctors"}
else(D$NDegree[i] <- D$NDEGREE[i])}
Bachelors <- D[grep("Bachelor", D$NDEGREE),]
BS <- D[grep("BS", D$NDEGREE),]
Masters <- D[grep("Master", D$NDEGREE),]
Doctors <- D[grep("Doctor", D$NDEGREE),]
EDIT: I also tried
D$NDEGREE <- gsub("Bachelor", "Bachelors", D$NDEGREE)
D$NDEGREE <- gsub("BS", "Bachelors", D$NDEGREE)
D$NDEGREE <- gsub("Master", "Masters", D$NDEGREE)
D$NDEGREE <- gsub("Doctor", "Doctors", D$NDEGREE)
This just runs through but nothing happens. The for if statement doesnt work. it just keeps running indefinitely.