【发布时间】:2020-02-07 03:13:44
【问题描述】:
我有一个名为d 的data.frame。在此 data.frame 中,某些列由跨第一列的行的常量组成:study.name(见下文)。
例如,ESL、ESL.1、prof 和 prof.1 列对于 Shin.Ellis 的所有行都是常量,对于 Trus.Hsu 的所有行也是常量,依此类推。
问: 在BASE R中,如何将这些常量变量分开,然后将它们压缩成一行只有一个数字?
我想要的输出如下所示。功能性答案表示赞赏。
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 !!!
【问题讨论】:
标签: r function loops dataframe