Rilcon42
Rilcon42

Reputation: 9763

recode values into one column

I have a dataframe with one value per row, potentially in one of several columns. How can I create a single column that contains the column number the 1 is in? I would like to do this using dplyr, but the only methods I can think of involve for loops, which seems very not R like.

df<-data.frame(
  a=c(1,0,0,0),
  b=c(0,1,1,0),
  c=c(0,0,0,1)
)

  a b c
1 1 0 0
2 0 1 0
3 0 1 0
4 0 0 1

GOAL:

1 1
2 2
3 2
4 3

Upvotes: 2

Views: 94

Answers (3)

Ven Yao
Ven Yao

Reputation: 3710

which(df==1, arr.ind=T)
#      row col
# [1,]   1   1
# [2,]   2   2
# [3,]   3   2
# [4,]   4   3

Upvotes: 0

akrun
akrun

Reputation: 887223

We could also do

as.matrix(df) %*%seq_along(df)
#     [,1]
#[1,]    1
#[2,]    2
#[3,]    2
#[4,]    3

Upvotes: 1

Rich Scriven
Rich Scriven

Reputation: 99341

There is no need for dplyr here. This is what max.col() is for. Since all the other values in the row will be zero, then max.col() will give us the column number where the 1 appears.

max.col(df)
# [1] 1 2 2 3

If you need a column, then

data.frame(x = max.col(df))
#   x
# 1 1
# 2 2
# 3 2
# 4 3

Or cbind() or matrix() for a matrix.

Upvotes: 6

Related Questions