blackknight316 blackknight316 - 4 months ago 20
R Question

R - require clarification on whats happening with the code

The details of the problem is listed here in this link

I couldn't not understand whats happening in the following lines of code:

Elite=rep("No",nrow(college))
Elite[college$Top10perc >50]=" Yes"
Elite=as.factor(Elite)
college=data.frame(college ,Elite)


I understand the line 2 and 3, but the rest seems to be mystery. I would really appreciate any help on this subject.

Thanks,

Yogi

Answer

Do the included comments clarify your questions?

#Create a vector of "No" of length equal to the number of rows in the dataframe college
Elite=rep("No",nrow(college))

#Subset the vector with the conditions of the column Top10perc > 50
#in the dataframe college and then set values to "YES"
Elite[college$Top10perc >50]=" Yes"

#Turn the Yes/No character strings into factors
Elite=as.factor(Elite)

#column bind the original dataframe with the newly created vector
#to update the original dataframe college.
college=data.frame(college ,Elite)
Comments