sapply() returns list instead of a vector

Viewed 6516

I have the following data frame -

   name amarks bmarks cmarks
1   A    25      30     40   
2   B    45      78     50 
3   C    75      72     29 
4   D    18      16     70  
.   .    .       .      .

Where name is the name of the person, amarks, bmarks and cmarks are the marks scored at different exams by the person. Now I am tasked to find out name's of the people who have scored max in amarks, bmarks and cmarks. Also I got to store it as a vector. I have solved it in the following way -

> max_name <- sapply(marks[,2:4], function(x) {subset(marks, x == max(x,
> na.rm = T), name)})

This gives me the correct answers but when I check the data type of max_name, I see that its a list when ideally I expected sapply to return vector.

Following are my observations -

class(max_name)

> list

typeof(max_name)

> list

is.vector(max_name)

> vector

Can somebody please explain what is happening over here. Am I missing something. Do I need to make any changes to my code so that it returns a vector?

1 Answers
Related