这是一个玩具数据框。
self.next_track
我想将前三列分为Player
,将后三列分为self.player = audiotools.player.Player(
audio_output,
replay_gain,
self.next_track)
# ^ no call parentheses
,以便我得到以下内容
>library(tidyverse)
>a
id e0 e1 e2 ee
1 0 1 2 3
1 0 1 2 3
1 0 1 2 3
2 6 7 8 9
2 6 7 8 9
2 6 7 8 9
答案 0 :(得分:2)
txt <- "id e0 e1 e2 ee
1 0 1 2 3
1 0 1 2 3
1 0 1 2 3
2 6 7 8 9
2 6 7 8 9
2 6 7 8 9"
a <- read.table(text = txt, header = TRUE)
使用dplyr
:
library(dplyr)
a2 <- distinct(a)
bind_rows(
select(a2, id, start = e0, end = e1),
select(a2, id, start = e1, end = e2),
select(a2, id, start = e2, end = ee)
)
# id start end
# 1 1 0 1
# 2 2 6 7
# 3 1 1 2
# 4 2 7 8
# 5 1 2 3
# 6 2 8 9
以基地R:
do.call("rbind.data.frame",
list(
setNames(a2[,c("id","e0","e1")], c("id", "start", "end")),
setNames(a2[,c("id","e1","e2")], c("id", "start", "end")),
setNames(a2[,c("id","e2","ee")], c("id", "start", "end"))
))
修改:根据评论,如果可以安全地假设每个id
的行数与e
- 列减去1的行数完全相同,那么你可以这样做:
nc <- 3
a %>%
group_by(id) %>%
mutate(
n = (row_number() - 1) %% nc + 1,
start = recode(n, e0, e1, e2),
end = recode(n, e1, e2, ee)
) %>%
ungroup() %>%
select(id, start, end)
# # A tibble: 6 × 3
# id start end
# <int> <int> <int>
# 1 1 0 1
# 2 1 1 2
# 3 1 2 3
# 4 2 6 7
# 5 2 7 8
# 6 2 8 9
实际上,即使没有正确的行数,这也有效,但如果你不这样做,这可能是不正确的结果。
答案 1 :(得分:1)
基本R选项是,
data.frame(ID = a$id, start = unique(c(t(a[2:4]))), end = unique(c(t(a[3:5]))))
# ID start end
#1 1 0 1
#2 1 1 2
#3 1 2 3
#4 2 6 7
#5 2 7 8
#6 2 8 9