R-根据与现有列匹配的值索引创建新的列数据框 [英] R - create new column dataframe based on index of value match with existing column
问题描述
我有一个名为imp2
的数据帧(大约6,000行),其中有9列标记为'savres1'...'savres9'
的列.这些列中每列的值为1或0.对于每一行,这些列中只有1的值为1(其余均为0).
I have a dataframe called imp2
(with about 6,000 rows) for which there are 9 columns labeled 'savres1'...'savres9'
. Values in each of these columns are either 1 or 0. For each row, the value of only 1 of these columns is 1 (rest are 0).
我想创建一个名为'savres'
的新列,在该列中放置一个1到9之间的值,该值与保存1值的列的索引(在这组9列中)匹配.例如.如果'savres7'
为1,其余列为0,则savres
应该等于7.
I'd like to create a new column called 'savres'
into which I would put a value between 1 and 9 that matches the index of the column (within this set of 9 columns) which holds the 1 value. E.g. if 'savres7'
is 1 and the rest of these columns are 0, then savres
should equal 7.
我使用了which
,match
和%in%
的几种变体来找到值,但没有得到我期望的输出.
I've used several variations of which
, match
and %in%
to find the value, but I'm not getting the output I expect.
下面是最近的试用功能,以及在数据框中创建的示例行集.
Below is a recent trial function and a sample set of rows that are created in the dataframe.
最近的尝试解决方案:
imp2 <- within(imp2, savres <- which(c(savres1, savres2, savres3, savres4, savres5, savres6, savres7, savres8, savres9) == 1) %% 9 + 1)
结果:
(注意,我没有包括所有列,但是从这里的第4列中,您可以看到该函数不起作用-第4行中的'savres'
值应为3,而第1行和第2行的值应为不是4或2)
(Note, I didn't include all columns, but from the 4 here, you can see that the function isn't working - the 'savres'
value in row 4 should be 3 and those for rows 1 and 2 should not be 4 or 2)
sample <- head(imp2[c('savres1','savres2','savres3','savres4')],4)
savres | savres1 | savres2 | savres3 | savres4
4 | 0 | 0 | 0 | 0
2 | 0 | 0 | 0 | 0
1 | 1 | 0 | 0 | 0
1 | 0 | 0 | 1 | 0
任何人都可以推荐解决方案吗?
Can anyone recommend a solution?
我认为这应该是一个相当简单的操作,但是我对尝试过的许多方法感到沮丧.
I thought this should be a fairly simple operation, but I've been frustrated with a number of approaches I've tried.
推荐答案
我们可以使用max.col
i1 <- grep("^savre", names(df1))
transform(df1, savre = (max.col(df1[i1], "first"))* !!rowSums(df1[i1]))
# col1 col2 savres1 savres2 savres3 savres4 savre
#1 1 2 0 0 0 0 0
#2 2 3 0 0 0 0 0
#3 3 4 1 0 0 0 1
#4 4 5 0 0 1 0 3
数据
df1 <- data.frame(col1 = 1:4, col2 = 2:5, savres1 = c(0, 0, 1,0),
savres2 = 0, savres3 = c(0, 0, 0, 1), savres4 = 0)
这篇关于R-根据与现有列匹配的值索引创建新的列数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!