简体   繁体   中英

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

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.

We could also do

as.matrix(df) %*%seq_along(df)
#     [,1]
#[1,]    1
#[2,]    2
#[3,]    2
#[4,]    3
which(df==1, arr.ind=T)
#      row col
# [1,]   1   1
# [2,]   2   2
# [3,]   3   2
# [4,]   4   3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM