R.R
R.R

Reputation: 9

How to use loop in R for big data set

Folks, Today I have joined this community and I have a problem in using loop in R My sample data set is as follows:-

Roll No     Marks
   1          60
   2          78
   3          84

I want a column to be added in this data set which will calculate grade of student.For eg If marks is more than 90 then A, 70-90 then B , less than 70 c. I want my out as:-

Roll No       Marks     Grade
   1            60        C
   2            78        B
   3            84        B

Is it possible to do it In R using loop , because i have around 60 thousand of data

Any help will be highly appreciated

Upvotes: 1

Views: 152

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61174

Here's a solution using ifelse, although using cut as suggested by @Ananda could be better

> transform(df, Grade= ifelse(Marks>90, "A", 
                              ifelse(Marks>=70 & Marks <=90, "B", "C")))
  Roll_No Marks Grade
1       1    60     C
2       2    78     B
3       3    84     B

Upvotes: 1

Related Questions