用常数分隔列并将其压缩到R data.frame中的一行 [英] Separate columns with constant numbers and condense them to one row in R data.frame
问题描述
我有一个名为 d
的data.frame。在此data.frame中,某些列由第一列各行中的常数组成: study.name
(请参见下文)。
I have a data.frame called d
. In this data.frame, some columns consist of constant numbers across the rows of the first column: study.name
(see below).
例如,列 ESL
, ESL.1
, prof
和 prof.1
是 Shin.Ellis
的所有行的常数,并且也是常数对于 Trus.Hsu
的所有行,等等。
For example, columns ESL
, ESL.1
, prof
, and prof.1
are constant numbers for all rows of Shin.Ellis
and also constant for all rows of Trus.Hsu
and so on.
Q: 在BASE R中,如何分隔这样的常量变量,然后仅用一个数字将它们压缩为一行?
Q: In BASE R, how can I separate such constant variables, and then condense them to one row with only one number?
我想要的输出如下所示。功能性的答案值得赞赏。
My desired output is shown further below. A functional answer is appreciated.
d <- read.csv("https://raw.githubusercontent.com/izeh/m/master/irr.csv", h = T)[-(2:3)]
## FIRST 8 ROWS:
# study.name ESL prof scope type ESL.1 prof.1 scope.1 type.1
# 1 Shin.Ellis 1 2 1 1 1 2 1 1
# 2 Shin.Ellis 1 2 1 1 1 2 1 1
# 3 Shin.Ellis 1 2 1 2 1 2 1 1
# 4 Shin.Ellis 1 2 1 2 1 2 1 1
# 5 Shin.Ellis 1 2 NA NA 1 2 NA NA
# 6 Shin.Ellis 1 2 NA NA 1 2 NA NA
# 7 Trus.Hsu 2 2 2 1 2 2 1 1
# 8 Trus.Hsu 2 2 NA NA 2 2 NA NA
所需的输出:
# study.name ESL prof ESL.1 prof.1
# 1 Shin.Ellis 1 2 1 2
# 2 Trus.Hsu 2 2 2 2
# . . . . . . # AND SO ON !!!
推荐答案
也许我们需要
library(dplyr)
d %>%
group_by(study.name) %>%
slice(1)
或以为基础R
按'study.name'分组后,在指定 na.action = NULL
的同时获取第一行,因为默认选项为 na.omit
可以省略任何列中具有 NA
的行
Or in base R
after grouping by 'study.name', get the first row while specifying the na.action = NULL
as the default option is na.omit
which can omit any row having NA
in any of the columns
aggregate(.~ study.name, d, head, 1, na.action = NULL)
如果我们想对列进行子集化
If we want to subset the columns
nm1 <- names(which(!colSums(!do.call(rbind, by(d[-1], d$study.name,
FUN = function(x) lengths(sapply(x, unique)) == 1)))))
unique(d[c("study.name", nm1)])
这篇关于用常数分隔列并将其压缩到R data.frame中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!