Vincent
Vincent

Reputation: 8856

Summing columns of characters in R data frame to create a new column

I currently have a data frame in R, where each entry is a character. However, each character also corresponds to a point value, where: B = 10, S = 1, C = 1, X = 0.

For example, consider the following data frame

> df = data.frame(p1 = c("B", "B", "C", "C", "S", "S", "X"), p2 = c("X", "B", "B", "S", "C", "S", "X"), p3 = c("C", "B", "B", "X", "C", "S", "X"))
> df
  p1 p2 p3
1  B  X  C
2  B  B  B
3  C  B  B
4  C  S  X
5  S  C  C
6  S  S  S
7  X  X  X

I want to create three new columns in R: c1, c2, c3 where these are essentially the "lagged" sum (using the numeric values of each characters) of the p1, p2, and p3 values.

  p1 p2 p3 c1 c2 c3
1  B  X  C 0  10 10
2  B  B  B 0  10 20
3  C  B  B 0  1  11
4  C  S  X 0  1  2
5  S  C  C 0  1  2
6  S  S  S 0  1  2
7  X  X  X 0  0  0

For example, c1 is always initialized to 0. c2 will be the point value of p1, and c3 will be the sum of c2 and the point value of p1.

In general c_i = c_{i-1} + p_{i-1}.

Is there an easy way to do this in R? Thank you in advance, as I am a relatively novice R user.

Upvotes: 0

Views: 937

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99371

Something like this would work. matchFun is a function that does the matching.

matchFun <- function(x) c(10, 1, 1, 0)[x]

within(df, {
    c3 <- rowSums(sapply(list(p1, p2), matchFun))
    c2 <- matchFun(p1)
    c1 <- 0L
})
#   p1 p2 p3 c1 c2 c3
# 1  B  X  C  0 10 10
# 2  B  B  B  0 10 20
# 3  C  B  B  0  1 11
# 4  C  S  X  0  1  2
# 5  S  C  C  0  1  2
# 6  S  S  S  0  1  2
# 7  X  X  X  0  0  0

Upvotes: 2

Related Questions