[英]Is there an R function to transform rows into vectors based on a grouping of three variables?
截至目前,我的數據在一個巨大的列表中,每三行數據包含相同觀察的信息。 這意味着當我操作數據框時,它們需要作為一個整體保持在一起。 這是我面臨的問題,我想將我的數據從行轉換為列,但我只想要三列(即,每種類型的信息都有一列)。
為了說明這是我現在的數據框:
1
00:09:49.350 --> 00:09:49.800
Hello, good.
2
00:09:54.750 --> 00:09:55.350
Customer: Morning.
3
00:09:56.100 --> 00:09:56.670
Morning.
這就是我所希望的:
1 00:09:49.350 --> 00:09:49.800 Hello, good.
2 00:09:54.750 --> 00:09:55.350 Customer: Morning.
3 00:09:56.100 --> 00:09:56.670 Morning.
感謝您的時間!
使用cbind
#sample data
df <- structure(list(X1 = c("1", "00:09:49.350 --> 00:09:49.800", "Hello, good.",
"2", "00:09:54.750 --> 00:09:55.350", "Customer: Morning.", "3",
"00:09:56.100 --> 00:09:56.670", "Morning.")), class = "data.frame", row.names = c(NA,
-9L))
現在像這樣進行
n1 <- nrow(df)
as.data.frame(cbind(df[1:n1 %% 3 == 1,], df[1:n1 %% 3 == 2,], df[1:n1 %% 3 == 0,]))
V1 V2 V3
1 1 00:09:49.350 --> 00:09:49.800 Hello, good.
2 2 00:09:54.750 --> 00:09:55.350 Customer: Morning.
3 3 00:09:56.100 --> 00:09:56.670 Morning.
說明使用 mod function 即R
中的%%
我們可以輕松地將cbind
從第一行、第二行和第三行開始分組,最后將它們全部組合在一起。 as.data.frame()
將根據需要將結果放入 data.frame,這是可選的。
此后,您可以輕松地操作列類型
這是 dplyr 的解決方案:
library(dplyr)
# the data as list just for explanation
my_list <- tibble(1,
"00:09:49.350 --> 00:09:49.800", "Hello, good.", 2, "00:09:54.750 --> 00:09:55.350",
"Customer: Morning.", 3, "00:09:56.100 --> 00:09:56.670", "Morning." )
# list to dataframe: **here replace my_list with your list**
df <- data.frame(matrix(unlist(my_list), nrow=length(my_list), byrow=TRUE))
# new dataframe
df1 <- df %>%
rename_at( 1, ~"Col1" ) %>% # rename column
mutate(id = rep(1:3, each=3)) %>% # group id for the data
group_by(id) %>%
slice(2:n()) %>% # remove first row in each group
mutate(Col2 = lead(Col1)) %>%
slice(1:(n()-1)) %>% # remove last row in each group
ungroup() %>%
select(-id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.