0

Assume the dataframe is like below:

   Name Score 
1  John  10
2  John   2
3  James  5

I would like to compute the mean of all the Score values which has John's name.

1
  • mean(df[df$Name == "John", "Score"]) where df is the data Commented Jul 26, 2015 at 4:11

2 Answers 2

1

You can easily perform a mean of every person's score with aggregate:

> aggregate(Score ~ Name, data=d, FUN=mean)
   Name Score
1 James     5
2  John     6
0
0

Using dplyr:

For each name:

library(dplyr)
df %>%
  group_by(Name) %>%
  summarise(Value=mean(Score))

   Name Value
1 James     5
2  John     6

Filtering:

filter(df, Name=="John") %>%
  group_by(Name) %>%
  summarise(Value=mean(Score))

  Name Value
1 John     6

Using sqldf:

library(sqldf)
sqldf("SELECT Name, avg(Score) AS Score
      FROM df GROUP BY Name")

   Name Score
1 James     5
2  John     6

Filtering:

sqldf("SELECT Name, avg(Score) AS Score
      FROM df 
      WHERE Name LIKE 'John'")

  Name Score
1 John     6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.