Home > Back-end >  How to count columns with specific values
How to count columns with specific values

Time:10-29

So I have a memory test with symbols and values. The correct values are saved in an order like 2, 4, 6, 8, 10, 12. If all are correct, I want to add a new column with the value 6. If for example for the 2 is a 3, then the new column should only contain 5. I hope you understand what I am trying to do. the correct order is

2 4 6 8 10 12

Answers

3 4 6 8 10 12 --> first is false
2 10 6 8 10 12 --> second is false
2 4 6 8 10 12 --> all correct

Now I want to add a new column and count the number of correct. This would look like this:

5
5
6

CodePudding user response:

It's always easier with a reproducible example, but from your description, your data are held in a data frame (let's call it df) and look something like this:

df
#>           first second third fourth fifth sixth
#> Subject 1     3      4     6      8    10    12
#> Subject 2     2     10     6      8    10    12
#> Subject 3     2      4     6      8    10    12

In which case, you can add a column giving the total score like this:

df$Score <- apply(df, 1, function(x) sum(x == 2 * 1:6))

So now your data frame looks like this:

df
#>           first second third fourth fifth sixth Score
#> Subject 1     3      4     6      8    10    12     5
#> Subject 2     2     10     6      8    10    12     5
#> Subject 3     2      4     6      8    10    12     6

Data used

df <- structure(list(first = c(3L, 2L, 2L), 
                     second = c(4L, 10L, 4L), 
                     third = c(6L, 6L, 6L), 
                     fourth = c(8L, 8L, 8L), 
                     fifth = c(10L, 10L, 10L), 
                     sixth = c(12L, 12L, 12L)), 
                class = "data.frame", 
                row.names = c("Subject 1", "Subject 2", "Subject 3"))

CodePudding user response:

The function below takes your input (x) and confront it to a correct vector. You can set the argument order to TRUE if the order in the input vector matters. See the 4 examples below:

# 4 dummy vectors to test the function
v1 <- c(3, 4, 6, 8, 10, 12)
v2 <- c(2, 5, 6, 8, 10, 12)
v3 <- c(2, 4, 6, 8, 10, 12)
v4 <- c(4, 2, 6, 8, 10, 12)# A 4th example where the order is reversed

# Write a function. You can edit what "correct" looks like.
# x is your object, and order is a logical argument.
# When order = FALSE (default), what counts is that all the values in your vector are in the "correct" vector.
# When order = TRUE, the order must be respected too.
check <- function(x, order = FALSE) {
  correct <- seq(2, 12, by = 2)
  if(!order) {
    return(length((x %in% correct)[(x %in% correct)])) 
  } else {
    return(length(which(c(order(v4) - order(correct)) == 0)))
  }
}

check(v1) # [5]
check(v2) # [5]
check(v3) # [6]
check(v4) # [6]
check(v4, order = TRUE) # [4]

If your vectors are rows of a dataframe:

# Your dataframe
df <- data.frame(t(matrix(c(v1, v2, v3, v4), ncol = v4, 
                          dimnames = list(c("first", "second", "third", "fourth", "fith", "sixth"), c("v1", "v2", "v3", "v4")))))

# Apply the function check() on every rows:
df$score <- apply(df, 1, function(x) check(x))
df$score_order <- apply(df, 1, function(x) check(x, order = TRUE))

Which in turns

df

#    first second third fourth fith sixth score score_order
# v1     2      5     6      8   10    12     5           4
# v2     2      5     6      8   10    12     5           4
# v3     4      2     6      8   10    12     6           4
# v4     4      2     6      8   10    12     6           4
  •  Tags:  
  • r
  • Related