I know how to add columns but I don't understand what the directions are asking me so I'll post them below.
This is the data set that's called Example.Data
I need to add a new column with these rules to my existing data set
12 <= Educ < 16: “HighSchool”
16 <= Educ < 17: “College”
17 <= Educ < 19: “Masters”
Educ >= 19: “Doctorate”```
Income Career Age Sex Married
1. 39540 Lawyer 20 F N
2. 45098 Teacher 65 F y
3. 54565 Doctor 45 M y
4. 48356 Teacher 26 M N
5. 68959 Nurse 32 F y
6. 98459 Lawyer 34 F Y
7. 34343 Nurse 49 M N
I will assume there is a column of data named "Educ" you haven't included (just using first four rows):
Income | Career | Age | Sex | Married | Educ |
---|---|---|---|---|---|
39540 | Lawyer | 20 | F | N | 18 |
45098 | Teacher | 65 | F | Y | 15 |
54565 | Doctor | 45 | M | Y | 22 |
48356 | Teacher | 26 | M | N | 16 |
Code:
Example.Data$Degree = ""
Example.Data[Example.Data$Edu>=12 & Example.Data$Edu<16,"Degree"] = "HighSchool"
Example.Data[Example.Data$Edu>=16 & Example.Data$Edu<17,"Degree"] = "College"
Example.Data[Example.Data$Edu>=17& Example.Data$Edu<19,"Degree"] = "Masters"
Example.Data[Example.Data$Edu>=19,"Degree"] = "Doctorate"
View(Example.Data)
Resulting in:
Income | Career | Age | Sex | Married | Educ | Degree |
---|---|---|---|---|---|---|
39540 | Lawyer | 20 | F | N | 18 | Masters |
45098 | Teacher | 65 | F | Y | 15 | HighSchool |
54565 | Doctor | 45 | M | Y | 22 | Doctorate |
48356 | Teacher | 26 | M | N | 16 | College |
There are more elegant ways of doing this but this is base R and is very clear what you are doing!