问题描述
我有一个难题,非常感谢您的协助.我需要编写一段代码,以便将其写成一行以适应更大的自动化流程.我提供了一些虚拟数据来帮助说明.
I have a r conundrum and would be very grateful of any assistance please. I need to write a piece of code that requires to be written one line to fit with a larger automated process. I have supplied some dummy data to help illustrate.
我有三个ifelse语句,它们返回1或0.我需要对这1和0进行求和,但是由于实际数据中存在其他继承的约束,因此我无法引用它们的输出然后对它们求和".我需要即时对其进行总结.
I have three ifelse statements that return 1’s or 0’s. I need to sum these 1’s and 0’s yet because of other inherited constraints in my real data I can’t refer to their output ‘and then’ sum them. I ‘need’ to sum them on the fly.
为明确起见……我无法明确地引用"use_sms","use_data"或"use_voice"的输出1和0,并且我不能仅将apply/1/sum传递给数据框.
To be explicit… I cannot explicitly refer to the output 1’s and 0’s of either ‘use_sms’, ‘use_data’ or ‘use_voice’ and I cannot just pass an apply/1/sum to the dataframe.
不知何故,我需要的是三个ifelse的完全包含的总和,大致类似于……用非r语言写成的……
Somehow, what I need is a fully contained sum of the three ifelse’s, something along the lines of… in crude non r language…
sum(
ifelse(sms_rev0 & sms_cnt0 > 0 | sms_rev1 & sms_cnt1 > 0 | sms_rev2 & sms_cnt2 > 0, 1, 0),
ifelse(data_rev0 & data_cnt0 > 0 | data_rev1 & data_cnt1 > 0 | data_rev2 & data_cnt2 > 0, 1, 0),
ifelse(voice_rev0 & voice_cnt0 > 0 | voice_rev1 & voice_cnt1 > 0 | voice_rev2 & voice_cnt2 > 0, 1, 0)
)
向我呈现的真实数据与此头痛_df相似
My real data is presented to me similar to this headache_df
headache_df = data.frame(sms_rev0 = sample(1:0, 10, replace = T),
sms_cnt0 = sample(1:0, 10, replace = T),
sms_rev1 = sample(1:0, 10, replace = T),
sms_cnt1 = sample(1:0, 10, replace = T),
sms_rev2 = sample(1:0, 10, replace = T),
sms_cnt2 = sample(1:0, 10, replace = T),
data_rev0 = sample(1:0, 10, replace = T),
data_cnt0 = sample(1:0, 10, replace = T),
data_rev1 = sample(1:0, 10, replace = T),
data_cnt1 = sample(1:0, 10, replace = T),
data_rev2 = sample(1:0, 10, replace = T),
data_cnt2 = sample(1:0, 10, replace = T),
voice_rev0 = sample(1:0, 10, replace = T),
voice_cnt0 = sample(1:0, 10, replace = T),
voice_rev1 = sample(1:0, 10, replace = T),
voice_cnt1 = sample(1:0, 10, replace = T),
voice_rev2 = sample(1:0, 10, replace = T),
voice_cnt2 = sample(1:0, 10, replace = T))
row.names(headache_df) = paste0("row", 1:10)
我希望在对抗panado_df的这种头痛中捕捉我的结果
And i am looking to capture my results in this headache combating panado_df
panado_df = data.frame(user = row.names(headache_df))
attach(headache_df)
set.seed(1234)
我生成了三个ifelse语句来说明,但是在我的真实数据中,这实际上是我需要捕获的这些语句的总和.
I generate three ifelse statements to illustrate but in my real data its really the sum of these I need to capture.
panado_df$use_sms = ifelse(sms_rev0 & sms_cnt0 > 0 | sms_rev1 & sms_cnt1 > 0 | sms_rev2 & sms_cnt2 > 0, 1, 0)
panado_df$use_data = ifelse(data_rev0 & data_cnt0 > 0 | data_rev1 & data_cnt1 > 0 | data_rev2 & data_cnt2 > 0, 1, 0)
panado_df$use_voice = ifelse(voice_rev0 & voice_cnt0 > 0 | voice_rev1 & voice_cnt1 > 0 | voice_rev2 & voice_cnt2 > 0, 1, 0)
rownames(panado_df) = panado_df$user
panado_df$user = NULL
我提供了一个目标列,以说明我的计算数据应该是什么样子.有什么好办法可以实现我的目标吗?
I present a target column to illustrate what my calculated data should look like. Any cool solutions to achieve my aim please?
panado_df$target_column = apply(panado_df, 1, sum)
推荐答案
如果我对您的理解正确,那么您可能正在寻找类似的东西
If I understand you correctly, you might be looking for something like this
panado_df$sums_3 <- sum(ifelse(sms_rev0 & sms_cnt0 > 0 | sms_rev1 & sms_cnt1 > 0 | sms_rev2 & sms_cnt2 > 0, 1, 0),
ifelse(data_rev0 & data_cnt0 > 0 | data_rev1 & data_cnt1 > 0 | data_rev2 & data_cnt2 > 0, 1, 0),
ifelse(voice_rev0 & voice_cnt0 > 0 | voice_rev1 & voice_cnt1 > 0 | voice_rev2 & voice_cnt2 > 0, 1, 0))
使用 dplyr
可以使您的代码更具描述性(就像您所做的那样)
And your code could be more descriptive (just like you did it) using dplyr
like follows
pando_df <- headach_df %>%
mutate(use_sms=ifelse(sms_rev0 & sms_cnt0 > 0 | sms_rev1 & sms_cnt1 > 0 | sms_rev2 & sms_cnt2 > 0, 1, 0),
use_data = ifelse(data_rev0 & data_cnt0 > 0 | data_rev1 & data_cnt1 > 0 | data_rev2 & data_cnt2 > 0, 1, 0),
use_voice = ifelse(voice_rev0 & voice_cnt0 > 0 | voice_rev1 & voice_cnt1 > 0 | voice_rev2 & voice_cnt2 > 0, 1, 0)) %>%
rowwise() %>%
mutate(target_column=sum(use_sms, use_data, use_voice))
,如果您想直接返回向量 target_column
,并添加 magrittr
库,请检查以下内容
and if you'd like to return the vector target_column
directly, adding magrittr
library, check the following
pando_df <- headach_df %>%
mutate(use_sms=ifelse(sms_rev0 & sms_cnt0 > 0 | sms_rev1 & sms_cnt1 > 0 | sms_rev2 & sms_cnt2 > 0, 1, 0),
use_data = ifelse(data_rev0 & data_cnt0 > 0 | data_rev1 & data_cnt1 > 0 | data_rev2 & data_cnt2 > 0, 1, 0),
use_voice = ifelse(voice_rev0 & voice_cnt0 > 0 | voice_rev1 & voice_cnt1 > 0 | voice_rev2 & voice_cnt2 > 0, 1, 0)) %>%
rowwise() %>%
mutate(target_column=sum(use_sms, use_data, use_voice)) %$%
target_column
这篇关于如何用[R]快速汇总ifelse语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!