0
我有1X2数据帧,其值为'sent1'和'sent2'。使用mutate从命名向量输出值
test.df <- data.frame(sentence = c('sent1', 'sent2'))
我也有一个参考向量,它具有2个句子和3个类别(a,b,c)组合的值。
test.vec <- c(sent1_a = 1,
sent1_b = 0,
sent1_c = 1,
sent2_a = 0,
sent2_b = 1,
sent2_c = 1)
我想创建一个新的DF,看起来像这样:
output.df <- data.frame(sentence = c('sent1', 'sent2'),
a = c(1,0),
b = c(0,1),
c = c(0,1))
output.df
# sentence a b c
#1 sent1 1 0 0
#2 sent2 0 1 1
理想情况下,我想用突变成基于相应的句子,从矢量选择相关的值,我'm循环穿过
results <- test.df %>%
mutate(a = test.vec[[paste0(sentence, '_a')]])
但是,我收到了一个错误。
Error in mutate_impl(.data, dots) : Evaluation error: attempt to select more than one element in vectorIndex.