Ajrhjnd
Ajrhjnd

Reputation: 330

Counting specific values of a column

I am working with R.

I have a data that look like this...

Condition  TargetWord             WordProduced        WPcondition                                                                   1          Target1                  table                 A
1          Target1                  word                  B
1          Target1                  chair                 A
1          Target1                  pole                  C
1          Target1                  skate                 D
1          Target2                  car                   B
1          Target2                  house                 A
1          Target2                  shoes                 A
1          Target2                  girl                  A
1          Target2                  life                  C
2          Target3                  computer              D
2          Target3                  ball                  B
2          Target3                  court                 F
2          Target3                  plane                 C
2          Target3                  sky                   D
2          Target4                  tree                  A
2          Target4                  five                  C
2          Target4                  help                  D
2          Target4                  shave                 A
2          Target4                  love                  B

I need to count the average of A's each target produced. Each target repeat itself five times. So it should the total of A's divided by 5. So I need a table like this...

Condition  TargetWord              MeanWPcondition                                                                   1          Target1                  .04                
1          Target2                  .06                  
2          Target3                  0                 
2          Target4                  .04      

Any help would be great. Thanks.

Upvotes: 0

Views: 74

Answers (4)

TarJae
TarJae

Reputation: 78917

A modifiction of the aggregate use (already presented by Ronak Shah):

MeanWPcondition = dat1$WPcondition =="A"

aggregate(MeanWPcondition ~ TargetWord, data=dat1, mean)

Output:

  TargetWord MeanWPcondition
1    Target1             0.4
2    Target2             0.6
3    Target3             0.0
4    Target4             0.4

Upvotes: 1

akrun
akrun

Reputation: 886938

In base R, we can use by. Just create a logical vector based on 'WPcondition', then specify the INDICES as the grouping column subset and apply the mean function

by(df$WPcondition == "A", df[c('Condition', 'TargetWord')], mean)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388807

In base R, you can do this with aggregate -

aggregate(WPcondition~Condition + TargetWord, df, function(x) mean(x == 'A', na.rm = TRUE))

#  Condition TargetWord WPcondition
#1         1    Target1         0.4
#2         1    Target2         0.6
#3         2    Target3         0.0
#4         2    Target4         0.4

Upvotes: 2

jared_mamrot
jared_mamrot

Reputation: 26484

Using tidyverse functions:

library(tidyverse)
dat1 <- data.frame(
  stringsAsFactors = FALSE,
                          Condition = c(1L,1L,1L,1L,1L,1L,1L,1L,1L,
                                        1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
                                        2L),
                         TargetWord = c("Target1","Target1","Target1",
                                        "Target1","Target1","Target2","Target2",
                                        "Target2","Target2","Target2",
                                        "Target3","Target3","Target3","Target3",
                                        "Target3","Target4","Target4","Target4",
                                        "Target4","Target4"),
                       WordProduced = c("table","word","chair","pole",
                                        "skate","car","house","shoes","girl",
                                        "life","computer","ball","court",
                                        "plane","sky","tree","five","help",
                                        "shave","love"),
                        WPcondition = c("A","B","A","C","D","B","A",
                                        "A","A","C","D","B","F","C","D",
                                        "A","C","D","A","B")
                 )
dat2 <- dat1 %>% 
  group_by(TargetWord) %>% 
  summarise(MeanWPcondition = mean(WPcondition == "A"))
dat2
## A tibble: 4 x 2
#  TargetWord MeanWPcondition
#  <chr>                <dbl>
#1 Target1                0.4
#2 Target2                0.6
#3 Target3                0  
#4 Target4                0.4

Upvotes: 2

Related Questions