I have 3 classes of students A, B, and C. My reproducible dataset looks like this:
data <- data.frame(Student_ID =c(1,1,1,2,2,3,3,3,3,3,4,4,4,5,6,6,7,7,7,8,8),
Years_Attended = c(1991,1992,1995,1992,1993,1991,1992,1993,1994,1995,1993,1994,1995,1995,1993,1995,1990,1995,2000,1995,1996),
Class = c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","C","C","C","C","C"))
I want to add a new column (New Student) to my data frame using R. I define “New student” as any student who did not attend in the first year (i.e., minimum year) of any given class. My intended out put looks like this:
Intended_output <- data.frame(Student_ID = c(1,1,1,2,2,3,3,3,3,3,4,4,4,5,6,6,7,7,7,8,8),
Years_Attended = c(1991,1992,1995,1992,1993,1991,1992,1993,1994,1995,1993,1994,1995,1995,1993,1995,1990,1995,2000,1995,1996),
Class = c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","C","C","C","C","C"),
New_Student = c("No","No","No","Yes","Yes","No","No","No","No","No","No","No","No","Yes","No","No","No","No","No","Yes","Yes"))
I have tried:
data$New_student <- ifelse(data$Years_Attended != min(data$Years_Attended, "Yes", "No"))
But I keep getting errors which I don't fully understand. I have been going in cycles for some time. I will really appreciate any pointers with this. The solution doesn't necessarily have to use the 'ifelse' statement. Any approach will certainly be appreciated.
Aucun commentaire:
Enregistrer un commentaire