Function with existing data frame

Hi, all.

I am working on some inter-coder reliability analyses for my dissertation meta-analysis. Myself ("TM") and another coder ("AY") coded data from a couple hundred study documents. I want to be able to create a function that allows me to enter in the study-document ID as an argument and then have the function output tell me whether or not there was a discrepancy in the coding decisions ("Funding," "Data") between myself and the other coder. I want to do something like the following, but I can't get the function to work.

Any help?

df <- tibble(
  ID = c( rep(1, 4), rep(2, 4)),
  Coder1 = c( rep( paste("TM"), 8)),
  Coder2 = c( rep( paste("AY"), 8)),
  Funding1 = rep(1, 8),
  Funding2 = c( rep(1, 4), rep(0, 4)),
  Data1 = c( rep(1, 4), rep(0, 4)),
  Data2 = rep(1, 8)
)

ndf <- df %>% 
  filter(ID == 1) #> I want to be able to enter the study ID (the document number)

unique( ndf$Funding1 == ndf$Funding2) #> And then have the function output whether or not
unique( ndf$Data1 == ndf$Data2)       #> we agreed/disagreed on a coding decision

I do not understand what it means to "agree on a coding decision" since the each ID appears four times in your data.

library(tibble)
df <- tibble(
  ID = c( rep(1, 4), rep(2, 4)),
  Coder1 = c( rep( paste("TM"), 8)),
  Coder2 = c( rep( paste("AY"), 8)),
  Funding1 = rep(1, 8),
  Funding2 = c( rep(1, 4), rep(0, 4)),
  Data1 = c( rep(1, 4), rep(0, 4)),
  Data2 = rep(1, 8)
)
df
#> # A tibble: 8 x 7
#>      ID Coder1 Coder2 Funding1 Funding2 Data1 Data2
#>   <dbl> <chr>  <chr>     <dbl>    <dbl> <dbl> <dbl>
#> 1     1 TM     AY            1        1     1     1
#> 2     1 TM     AY            1        1     1     1
#> 3     1 TM     AY            1        1     1     1
#> 4     1 TM     AY            1        1     1     1
#> 5     2 TM     AY            1        0     0     1
#> 6     2 TM     AY            1        0     0     1
#> 7     2 TM     AY            1        0     0     1
#> 8     2 TM     AY            1        0     0     1

Created on 2019-05-21 by the reprex package (v0.2.1)
Do you want the function to say whether the two coders agreed every time on ID == 1, or they agreed n out of m times, or something else?

Thanks for your reply!

I want the output to say, for ID == 1, is Funding1 == Funding2 and Data1 == Data2? The output could be for each individual row or for each set of rows (unique).

For example, Coder1 said ID == 2 did receive funding for their study, but Coder2 said ID == 2 did not receive funding. I want the output to point out where we disagreed on a coding decision. Does that make sense? The decision could be for each row/case or for the set of Funding/Data decisions for each coder.

I'm trying to do something like the following:

Output <- vector("logical", length = 2)

Fun_REP <- function(df, x){
  
x <- df$ID == x
ndf <- df %>% filter(df$ID == x)

d <- c( unique( ndf$Funding1 == ndf$Funding2),
        unique( ndf$Data1 == ndf$Data2))

Output[[i]] <- d[[i]]

}

Fun_REP(df, 1)
Fun_REP(df, 2)

Like this?

library(tibble)
df <- tibble(
  ID = c( rep(1, 4), rep(2, 4)),
  Coder1 = c( rep( paste("TM"), 8)),
  Coder2 = c( rep( paste("AY"), 8)),
  Funding1 = rep(1, 8),
  Funding2 = c( rep(1, 4), rep(0, 4)),
  Data1 = c( rep(1, 4), rep(0, 4)),
  Data2 = rep(1, 8)
)
df
#> # A tibble: 8 x 7
#>      ID Coder1 Coder2 Funding1 Funding2 Data1 Data2
#>   <dbl> <chr>  <chr>     <dbl>    <dbl> <dbl> <dbl>
#> 1     1 TM     AY            1        1     1     1
#> 2     1 TM     AY            1        1     1     1
#> 3     1 TM     AY            1        1     1     1
#> 4     1 TM     AY            1        1     1     1
#> 5     2 TM     AY            1        0     0     1
#> 6     2 TM     AY            1        0     0     1
#> 7     2 TM     AY            1        0     0     1
#> 8     2 TM     AY            1        0     0     1
Agree <- function(x, ID) {
  dat <- x[x$ID == ID, ]
  list(FundingAgree = sum(dat$Funding1 == dat$Funding2),
       DataAgree = sum(dat$Data1 == dat$Data2),
       N = nrow(dat))
}
Agree(df, 1)
#> $FundingAgree
#> [1] 4
#> 
#> $DataAgree
#> [1] 4
#> 
#> $N
#> [1] 4
Agree(df, 2)
#> $FundingAgree
#> [1] 0
#> 
#> $DataAgree
#> [1] 0
#> 
#> $N
#> [1] 4

Created on 2019-05-21 by the reprex package (v0.2.1)

2 Likes

WONDERFUL! Thanks so much for your help! This is exactly the type of function I was looking to write. Thanks a million. You just saved me days of beating my head against the wall.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.