如何使用dplyr的cocece函数与group_by()创建一个填充所有值的每一行?
我正在尝试使用cocece()每位参与者的一排,其名称和得分。参与者有3个填写数据的机会,大多数人只有一次(多次出现的人总是放入相同的数据)。因此,我的数据看起来像:
library(dplyr)
test_dataset <- tibble(name = c("justin", "justin", "justin", "corey", "corey", "corey", "sib", "sib", "sib", "kate", "kate", "kate"),
score1 = c(NA_real_, NA_real_, 1, 2, NA_real_, NA_real_, 2, NA_real_, 2, NA_real_, NA_real_ , NA_real_),
score2 = c(NA_real_, 7, NA_real_, 5, NA_real_, NA_real_, 9, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_))
我希望它看起来像:
library(dplyr)
answer <- tibble(name = c("justin", "corey", "sib", "kate"),
score1_true = c(1, 2, 2, NA),
score2_true = c(7, 5, 9, NA))
我尝试了以下解决方案,这确实给了我“ True”分数,但是它分布在12行(每人3行)上,而不是4(一个)人):
library(dplyr)
test_dataset %>%
dplyr::group_by(name) %>%
mutate(across(c(starts_with("score")), .fns = list(true = ~coalesce(.))))
I am trying to use coalesce() to produce one row per participant that has their name and their score. Participants had 3 opportunities to fill in their data, and most only came in once (and those that came in multiple times always put in the same data). So my data looks like:
library(dplyr)
test_dataset <- tibble(name = c("justin", "justin", "justin", "corey", "corey", "corey", "sib", "sib", "sib", "kate", "kate", "kate"),
score1 = c(NA_real_, NA_real_, 1, 2, NA_real_, NA_real_, 2, NA_real_, 2, NA_real_, NA_real_ , NA_real_),
score2 = c(NA_real_, 7, NA_real_, 5, NA_real_, NA_real_, 9, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_))
And I want it to look like:
library(dplyr)
answer <- tibble(name = c("justin", "corey", "sib", "kate"),
score1_true = c(1, 2, 2, NA),
score2_true = c(7, 5, 9, NA))
I've tried the below solution, which does give me the 'true' score, but it's spread out over 12 rows (3 rows per person) instead of 4 (one per person):
library(dplyr)
test_dataset %>%
dplyr::group_by(name) %>%
mutate(across(c(starts_with("score")), .fns = list(true = ~coalesce(.))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
fill()
,然后 repand> repand()分数并使用slice_head()
:输出:
更多简洁/改进版本,多亏了@m.viking:
.direction =“ up”
选项fill()
You can use
fill()
, and thenarrange()
the scores and useslice_head()
:Output:
more concise/improved version thanks to @M.Viking:
.direction="up"
option withinfill()
我们可以根据
na
元素对值进行重新排序,然后将第一行-Output
之后使用
complete.cases
切成 postif.cases 在重新安排-OUTPUT
We could reorder the values based on the
NA
elements and then slice the first row-output
Or another option is to use
complete.cases
after rearranging-output