Bridee123
Bridee123

Reputation: 21

R- Create function that selects entire row in data frame by column name

This is a question for an R Programming class, but I have been working on it for several hours, over a period of a few days. I have done internet searches and referenced three different books. I have tried very hard to solve it on my own. I am finally asking for help.

I was given a csv, which I read into the program. This is the resulting dataframe, named df:

   name hw0 hw1 hw2 hw3 hw4 hw5 hw6 quiz1 quiz2 quiz3 quiz4 quiz5 quiz6 term1 
1        20  14  30 100  50  60  36    12    15    30    15    25    25   100   
2     A  20  13  30 100  50  60  30    11    15     0    14    25    25   100   
3     B  20  14  30 100  50  60  36     8    11    24     8    13     9    95   
4     C  20  14  28 100  50  60  36    12     4    25    13    24    14    95   
5     D  20  12  30 100  50   0  33     7    15    26    12    22     0   100   
6     E  20  14  30  90  30   0   0    10    15    30    15    21    15   100   
7     F  20  13  30 100  48   0  36    12    15    30    15    25    23    95   
8     G  20  14  26  85  40  42  33    11    15    23    11    17    16    90   
9     H  20   0   0  85  50   0   0     0    15     0     0    15    10    85    
10    I  20  14  15   0  10  48  30    11     0    27    11    14    16    60    
11    J  20  14  29  80  35   0  36    11    13    24    12    14     0    70     
12    K  20  14  29  97  50  60  36     4     7    19    11    20    15   100   
13    L  20  14  30 100  45   0  36    10     6    26     8    16     7    80     
14    M  20  14  30 100  50  60  36     7    15    28    14    25    25   100   
15    N  20  11   0  95  20   0   0     8    14    26     7     9     0    95     
16    O  20  12  28  97   0  40   0    11    10    27    11    15    15    70     
17    P  20  13   0  90  45   0  20     4    13    30    10    20    17    90   
18    Q  20  14  30 100  45   0  36     0    12    21    11    14    17    75    
   term2 term3 exam1 exam2 exam3 final
1    100   100   100   100    95   100
2    100   100    97    97    80    97
3    100   100    83    85    73    73
4    100   100    88    75    56    77
5    100     0    90    87    72    81
6    100    80    92    82    69    79
7    100   100    90    95    87    90
8    100     0    89    79    81    78
9     90   100    62    83    42    75
10    90    72    78    78    66    81
11     0     0    79    77    51    78
12   100   100    79    77    57    81
13     0   100    68    74    76    76
14   100   100    99    98    82    99
15     0     0    70    70    52    61
16     0     0    63    66     0     0
17   100   100    75    72    56    64
18    90    75    72    84    54    63

QUESTION:

checkStudent <- function(df, studentName);

This function extracts a particular student's grades data from a data frame and returns them.

REQUIRED FORMAT:

checkStudent <- function(df, studentName)
{

}

TIPS PROVIDED:

inputs: df -- a data frame that contains all the grades data studentName -- name of a student

return: all the grades for the student whose name is given as studentName

purpose: extracting a particular student's grades data from a data frame and returning them

PROJECT TESTER- line of code and expected results:

checkStudent(df,"A")

   name hw0 hw1 hw2 hw3 hw4 hw5 hw6 quiz1 quiz2 quiz3 quiz4 quiz5
 2    A  20  13  30 100  50  60  30    11    15     0    14    25
   quiz6 term1 term2 term3 exam1 exam2 exam3 final
 2    25   100   100   100    97    97    80    97

I feel like I have been given everything and still can't get it right. I have tried:

checkStudent <- function(df, studentName)
{
  name <- studentName
  df["name", ] 
}

and

checkStudent <- function(df, studentName)
{
  subset(df, "name" == studentName, 1:21)
}

and numerous other lines of code, too many to list.

Please help. I am truly stuck.

Again, this needs to be done strictly in R. If it matters, I'm using RStudio. Thank you so much.

Upvotes: 2

Views: 177

Answers (2)

RHertel
RHertel

Reputation: 23788

Try with logical subsetting:

checkStudent <- function(x,y) x[x['name']==y,]

Test:

checkStudent(df,"A")
#  name hw0 hw1 hw2 hw3 hw4 hw5 hw6 quiz1 quiz2 quiz3 quiz4 quiz5 quiz6 term1 term2 term3 exam1 exam2 exam3 final
#1    A  20  13  30 100  50  60  30    11    15     0    14    25    25   100   100   100    97    97    80    97

data:

df <- structure(list(name = structure(1:17, .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", 
"Q"), class = "factor"), hw0 = c(20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L), hw1 = c(13L, 
14L, 14L, 12L, 14L, 13L, 14L, 0L, 14L, 14L, 14L, 14L, 14L, 11L, 
12L, 13L, 14L), hw2 = c(30L, 30L, 28L, 30L, 30L, 30L, 26L, 0L, 
15L, 29L, 29L, 30L, 30L, 0L, 28L, 0L, 30L), hw3 = c(100L, 100L, 
100L, 100L, 90L, 100L, 85L, 85L, 0L, 80L, 97L, 100L, 100L, 95L, 
97L, 90L, 100L), hw4 = c(50L, 50L, 50L, 50L, 30L, 48L, 40L, 50L, 
10L, 35L, 50L, 45L, 50L, 20L, 0L, 45L, 45L), hw5 = c(60L, 60L, 
60L, 0L, 0L, 0L, 42L, 0L, 48L, 0L, 60L, 0L, 60L, 0L, 40L, 0L, 
0L), hw6 = c(30L, 36L, 36L, 33L, 0L, 36L, 33L, 0L, 30L, 36L, 
36L, 36L, 36L, 0L, 0L, 20L, 36L), quiz1 = c(11L, 8L, 12L, 7L, 
10L, 12L, 11L, 0L, 11L, 11L, 4L, 10L, 7L, 8L, 11L, 4L, 0L), quiz2 = c(15L, 
11L, 4L, 15L, 15L, 15L, 15L, 15L, 0L, 13L, 7L, 6L, 15L, 14L, 
10L, 13L, 12L), quiz3 = c(0L, 24L, 25L, 26L, 30L, 30L, 23L, 0L, 
27L, 24L, 19L, 26L, 28L, 26L, 27L, 30L, 21L), quiz4 = c(14L, 
8L, 13L, 12L, 15L, 15L, 11L, 0L, 11L, 12L, 11L, 8L, 14L, 7L, 
11L, 10L, 11L), quiz5 = c(25L, 13L, 24L, 22L, 21L, 25L, 17L, 
15L, 14L, 14L, 20L, 16L, 25L, 9L, 15L, 20L, 14L), quiz6 = c(25L, 
9L, 14L, 0L, 15L, 23L, 16L, 10L, 16L, 0L, 15L, 7L, 25L, 0L, 15L, 
17L, 17L), term1 = c(100L, 95L, 95L, 100L, 100L, 95L, 90L, 85L, 
60L, 70L, 100L, 80L, 100L, 95L, 70L, 90L, 75L), term2 = c(100L, 
100L, 100L, 100L, 100L, 100L, 100L, 90L, 90L, 0L, 100L, 0L, 100L, 
0L, 0L, 100L, 90L), term3 = c(100L, 100L, 100L, 0L, 80L, 100L, 
0L, 100L, 72L, 0L, 100L, 100L, 100L, 0L, 0L, 100L, 75L), exam1 = c(97L, 
83L, 88L, 90L, 92L, 90L, 89L, 62L, 78L, 79L, 79L, 68L, 99L, 70L, 
63L, 75L, 72L), exam2 = c(97L, 85L, 75L, 87L, 82L, 95L, 79L, 
83L, 78L, 77L, 77L, 74L, 98L, 70L, 66L, 72L, 84L), exam3 = c(80L, 
73L, 56L, 72L, 69L, 87L, 81L, 42L, 66L, 51L, 57L, 76L, 82L, 52L, 
0L, 56L, 54L), final = c(97L, 73L, 77L, 81L, 79L, 90L, 78L, 75L, 
81L, 78L, 81L, 76L, 99L, 61L, 0L, 64L, 63L)), .Names = c("name", 
"hw0", "hw1", "hw2", "hw3", "hw4", "hw5", "hw6", "quiz1", "quiz2", 
"quiz3", "quiz4", "quiz5", "quiz6", "term1", "term2", "term3", 
"exam1", "exam2", "exam3", "final"), row.names = c(NA, -17L), class = "data.frame")

Upvotes: 1

bigfoot56
bigfoot56

Reputation: 69

You're really close.

Variables in R should never be encapsulated in quotes, but always are free standing. Additionally your code is just printing the row, it is not returning it.

Here's a slightly modify version of your first attempt, without the quotes.

checkStudent <- function(df, studentName)
{
  name <- studentName
  return(df[name, ])
}

Edit: Oops, I realized your rows aren't named as the students.

You'll need to make it more like this:

checkStudent <- function(df, studentName)
{
  my_row <- which(df$name == studentName)
  return(df[my_row, ])
}

Upvotes: 2

Related Questions