R - 需要澄清代码中发生的事情

时间:2016-08-04 19:43:31

标签: r

此问题的详细信息列在此link

我无法理解以下几行代码中发生的事情:

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

我理解第2行和第3行,但其余部分似乎很神秘。我真的很感激这方面的任何帮助。

谢谢,

瑜珈

1 个答案:

答案 0 :(得分:0)

包含的评论是否澄清了您的问题?

#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)