【发布时间】:2015-09-29 13:29:09
【问题描述】:
让我们先获取一些随机数据
A <- c(1:5)
score_one <- c(123.5, 223.1, 242.2, 351.8, 123.1)
score_two <- c(324.2, 568.2, 124.9, 323.1, 213.4)
score_three <- c(553.1, 412.3, 435.7, 523.1, 365.4)
score_four <- c(123.2, 225.1, 243.6, 741.1, 951.2)
df1 <- data.frame(A, score_one, score_two, score_three, score_four)
library(dplyr)
library(tidyr)
df2 <- df1 %>%
group_by(A) %>%
mutate_each(funs(substr(.,1,1))) %>%
ungroup %>%
gather(variable, type, -c(A)) %>%
select(-variable) %>%
mutate(type = paste0("type_",type),
value = 1) %>%
group_by(A,type) %>%
summarise(value = sum(value)) %>%
ungroup %>%
spread(type, value, fill=0) %>%
inner_join(df1, by=c("A")) %>%
select(A, starts_with("score_"), starts_with("type_"))
这为每个score_ 引入了一个汇总变量
并计算每个唯一第一位数字
因此我们在第一行看到 type_1 == 2。因为在相应的 score_ 列中我们有 2 次出现,其中数字 1 是第一个数字
问题陈述
现在我们要引入一个调用type_n 列的变量。
- 它检查值是否 > 0。
- 在这种情况下,我们要检查对应的
score_column/s - 这里我们分析小数点后位是否>=大于值2
- 现在如果一个或所有对应行的小数点后的值 >= 2,我们要分配一个值 1
- 如果所有对应行的小数点后的值都是我们要赋值为0
- 因此,如果
type_n == 0,我们要分配一个 0 - 假设我们将此变量命名为
$type_n_G2
这样所需的输出应该看起来像1
以type_1_G2为例
- 我们有
type_1 == 2 - 我们在
score_one和score_four有对应的身份 - 小数点后的两个值都>= 2,所以我们分配
type_1_G2==1
【问题讨论】:
-
我不明白想要的输出是什么。这里的代码和措辞太多,我看不出你真正想要实现的目标。
-
在你的例子中我不明白你为什么选择 score_one 和 score_four?既然您正在评估 type_1 不应该只是 score_one?
-
我们要检查
score_one和score_four,因为它们都以== 1开头 -
提供的数据集中的值与您的图像不匹配。第 1 行的
score_four是 123.1 而不是 123.2,第二行的 score_one 是 223.7 而不是 223.1。等等 -
投反对票,制作一个正确的输入示例,并制作一个包含所有边缘情况的预期输出,以便可以根据它验证答案。