In: Computer Science
Please do this operation in "R studio"
Please recall the vectors that we created for the topic "Data Frames".
name = c('Nikola','Albert', 'Marie','Isaac','Graham','Lise',
'Rosalind')
surname = c('Tesla','Einstein','Curie', 'Newton', 'Bell',
'Meitner', 'Franklin')
gender =
c('Male','Male','Female','Male','Male','Female','Female')
years = c(87,76,75,84,77,89,81)
field_of_study =
c('Engineering','Physics','Chemistry','Physics','Engineering','Physics','Chemistry')
Please check for the function "cut" and use it to create a data frame named "scientists" which has the values
name surname gender years field_of_study years_bin
1 Nikola Tesla Male 87 Engineering (80,90]
2 Albert Einstein Male 76 Physics (70,80]
3 Marie Curie Female 75 Chemistry (70,80]
4 Isaac Newton Male 84 Physics (80,90]
5 Graham Bell Male 77 Engineering (70,80]
6 Lise Meitner Female 89 Physics (80,90]
7 Rosalind Franklin Female 81 Chemistry (80,90]
where "years_bin" attribute is the bin of "years", either "70 to 80" or "80 to 90".
Then please check the function "tapply" to get the averages of the bins like
(70,80] (80,90]
76.00 85.25
Note : Use of functions and methods (such as loops, conditionals) that are not covered yet is forbidden
R CODE:
name = c('Nikola','Albert', 'Marie','Isaac','Graham','Lise', 'Rosalind')
surname = c('Tesla','Einstein','Curie', 'Newton', 'Bell', 'Meitner', 'Franklin')
gender = c('Male','Male','Female','Male','Male','Female','Female')
years = c(87,76,75,84,77,89,81)
field_of_study = c('Engineering','Physics','Chemistry','Physics','Engineering','Physics','Chemistry')
scientists<- data.frame(name,surname,gender,years,field_of_study)
scientists
yearsbin <- cut(scientists$years, breaks = 2, labels = c("70-80", "80-90"))
scientists$years_bin <- yearsbin
scientists
tapply(scientists$years,scientists$years_bin, FUN = mean)
Here the code image
OUTPUT IMAGE: