英文:
Row-wise Boolean comparison of data
问题
我已经按适当的分组对数据进行了分组,我需要确保每个唯一的 Group1 和 Group2 组合下的 "x" 和 "y" 值相等。换句话说,我可以使用什么代码循环遍历这个数据集,并确保 A1x == A1y,A2x == A2y,等等。
以下是示例中的数据:
"Group1","Group2","group3","values"
"A" "1" x 10
"A" "1" y 10
"A" "2" x 15
"A" "2" y 15
为了简化回答,以下是示例中的数据框:
d <- data.frame(Group1= c("A", "A", "A", "A"),
Group2= c("1", "1", "2", "2"),
group3= c("x", "y", "x", "y"),
values= c(10, 10, 15, 15))
英文:
I have grouped my data by the appropriate grouping, and I need to be sure that "x" and "y" values equal each other for each unique combination of Group1 and Group2. In other words, what code could I use to cycle through this dataset and ensure that A1x == A1y and A2x == A2y, etc.
"Group1","Group2","group3","values"
"A" "1" x 10
"A" "1" y 10
"A" "2" x 15
"A" "2" y 15
To help make the answer easier, here is the data.frame from the example
d <- data.frame(Group1= c("A", "A", "A", "A"),
Group2= c("1", "1", "2", "2"),
group3= c("x", "y", "x", "y"),
values= c(10, 10, 15, 15))
答案1
得分: 4
使用dplyr
,你可以做以下操作:
d %>%
group_by(Group1, Group2) %>%
mutate(cond = all(values == first(values)))
Group1 Group2 group3 values cond
<fct> <fct> <fct> <dbl> <lgl>
1 A 1 x 10 TRUE
2 A 1 y 10 TRUE
3 A 2 x 15 TRUE
4 A 2 y 15 TRUE
或者:
d %>%
group_by(Group1, Group2) %>%
mutate(cond = n_distinct(values) == 1)
英文:
With dplyr
, you can do:
d %>%
group_by(Group1, Group2) %>%
mutate(cond = all(values == first(values)))
Group1 Group2 group3 values cond
<fct> <fct> <fct> <dbl> <lgl>
1 A 1 x 10 TRUE
2 A 1 y 10 TRUE
3 A 2 x 15 TRUE
4 A 2 y 15 TRUE
Or:
d %>%
group_by(Group1, Group2) %>%
mutate(cond = n_distinct(values) == 1)
答案2
得分: 3
你也可以使用 pivot_wider
完成这个操作:
tidyr::pivot_wider(d, names_from='group3', values_from='values') %>%
dplyr::mutate(eq=x==y)
英文:
You can also do this with pivot_wider
:
tidyr::pivot_wider(d, names_from='group3', values_from='values') %>%
dplyr::mutate(eq=x==y)
答案3
得分: 1
我认为你在将数据转换为长格式方面走得太远,也许这样更容易操作:
d %>%
pivot_wider(names_from = group3, values_from = values) %>%
mutate(is_equal = x == y)
英文:
I think you went too far into turning your data into a long format maybe this is easier to manipulate
d %>%
pivot_wider(names_from = group3,values_from = values) %>%
mutate(is_equal = x == y)
答案4
得分: 1
以下是使用基本的 R 解决方案,使用 ave()
来实现的部分:
d <- within(d, isequal <- as.logical(ave(values, Group1, Group2, FUN = function(v) v == unique(v))))
这样,数据框 d
中的 isequal
列将如下所示:
> d
Group1 Group2 group3 values isequal
1 A 1 x 10 TRUE
2 A 1 y 10 TRUE
3 A 2 x 15 TRUE
4 A 2 y 15 TRUE
请注意,这段代码使用 ave()
函数根据 Group1
和 Group2
列的组合来计算 values
列是否在组合内是唯一的,并将结果存储在 isequal
列中。
英文:
Here is a base R solution using ave()
to make it
d <- within(d,isequal <- as.logical(ave(values,Group1,Group2,FUN = function(v) v==unique(v))))
such that
> d
Group1 Group2 group3 values isequal
1 A 1 x 10 TRUE
2 A 1 y 10 TRUE
3 A 2 x 15 TRUE
4 A 2 y 15 TRUE
答案5
得分: 0
另一种选项是,如果数据被正确分组并且每组有2行:
d$check <- rep(d$values[seq(1L, nrow(d), 2L)] == d$values[seq(2L, nrow(d), 2L)], each = 2L)
英文:
Another option if the data is grouped properly and has 2 rows for each group:
d$check <- rep(d$values[seq(1L,nrow(d),2L)]==d$values[seq(2L,nrow(d),2L)], each=2L)
答案6
得分: -1
一个简单的方法是合并具有组x和组y的子表格以比较数值。
> d[d$group3=="y",]
# Group1 Group2 group3 values
# 2 A 1 y 10
# 4 A 2 y 15
> merge(d[d$group3=="y",],d[d$group3=="x",],by=c("Group1","Group2"))
# Group1 Group2 group3.x values.x group3.y values.y
# 1 A 1 y 10 x 10
# 2 A 2 y 15 x 15
with(merge(d[d$group3=="y",], d[d$group3=="x",],
by=c("Group1","Group2")),
values.x==values.y)
## [1] TRUE TRUE
当然,你还有更高级的方法,但从简单开始并不是坏事。
<details>
<summary>英文:</summary>
A simple way would be to merge the sub tables with group x and group y to compare the values.
> d[d$group3=="y",]
# Group1 Group2 group3 values
# 2 A 1 y 10
# 4 A 2 y 15
> merge(d[d$group3=="y",],d[d$group3=="x",],by=c("Group1","Group2"))
# Group1 Group2 group3.x values.x group3.y values.y
# 1 A 1 y 10 x 10
# 2 A 2 y 15 x 15
with(merge(d[d$group3=="y",], d[d$group3=="x",],
by=c("Group1","Group2")),
values.x==values.y)
## [1] TRUE TRUE
Of course you have fancier ways of doing it but it is not bad to start simple first
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论