【发布时间】:2020-02-07 08:58:45
【问题描述】:
在 data.frame DATA 中,我有一些列是第一列的唯一行中的常数,称为 study.name。例如,对于Shin.Ellis 的所有行,列ESL 和prof 是constant,对于Trus.Hsu 的所有行,列是constant 等等。包括Shin.Ellis 和Trus.Hsu,共有8 个唯一的study.name 行。
但是在下面我的split.default() 调用之后,对于这样的常量,我如何才能为唯一的study.name 下的所有行获取一个数据点(例如,一个用于Shin.Ellis,一个用于Trus.Hsu 等)变量? (即总共 8 行)
例如,在我的split.default() 之后,所有名为ESL 的变量都显示只有8 行,每行对应一个唯一的study.name。
我想要的输出 ONLY ESL 和 prof 如下所示。
注意:这是玩具数据。我们首先应该找到常量变量。非常感谢功能性答案。
DATA <- read.csv("https://raw.githubusercontent.com/izeh/m/master/irr.csv", h = T)[-(2:3)]
DATA <- setNames(DATA, sub("\\.\\d+$", "", names(DATA)))
tbl <- table(names(DATA))
nm2 <- names(which(tbl==max(tbl)))
L <- split.default(DATA[names(DATA) %in% nm2], names(DATA)[names(DATA) %in% nm2])
## FIRST 8 ROWS of `DATA`:
# study.name ESL prof scope type ESL prof scope type
# 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
# . ... . . . . . . . . # `DATA` has 54 rows overall
ESL 和 prof 在split.default() 调用后的所需输出:
# $ESL ## 8 unique rows for 8 unique `study.name`
# ESL ESL.1
# 1 1 1
# 7 2 2
# 9 1 1
# 17 1 1
# 23 1 1
# 35 1 1
# 37 2 2
# 49 2 2
# $prof ## 8 unique rows for 8 unique `study.name`
# prof prof.1
# 1 2 2
# 7 2 2
# 9 3 3
# 17 2 2
# 23 2 2
# 35 2 2
# 37 NA NA
# 49 2 2
【问题讨论】:
标签: r list function loops dataframe