[英]Moving down columns in data frames in R
Suppose I have the next data frame: 假设我有下一个数据帧:
df<-data.frame(step1=c(1,2,3,4),step2=c(5,6,7,8),step3=c(9,10,11,12),step4=c(13,14,15,16))
step1 step2 step3 step4
1 1 5 9 13
2 2 6 10 14
3 3 7 11 15
4 4 8 12 16
and what I have to do is something like the following: 我要做的是如下所示:
df2<-data.frame(col1=c(1,2,3,4,5,6,7,8,9,10,11,12),col2=c(5,6,7,8,9,10,11,12,13,14,15,16))
col1 col2
1 1 5
2 2 6
3 3 7
4 4 8
5 5 9
6 6 10
7 7 11
8 8 12
9 9 13
10 10 14
11 11 15
12 12 16
How can I do that? 我怎样才能做到这一点? consider that more steps can be included (example, 20 steps). 考虑可以包含更多步骤(例如20个步骤)。
Thanks!! 谢谢!!
We can design a function to achieve this task. 我们可以设计一个功能来完成此任务。 df_final
is the final output. df_final
是最终输出。 Notice that bin
is an argument that the users can specify how many columns to transform together. 请注意, bin
是一个参数,用户可以指定要一起转换的列数。
# A function to conduct data transformation
trans_fun <- function(df, bin = 3){
# Calculate the number of new columns
new_ncol <- (ncol(df) - bin) + 1
# Create a list to store all data frames
df_list <- lapply(1:new_ncol, function(num){
return(df[, num:(num + bin - 1)])
})
# Convert each data frame to a vector
dt_list2 <- lapply(df_list, unlist)
# Convert dt_list2 to data frame
df_final <- as.data.frame(dt_list2)
# Set the column and row names of df_final
colnames(df_final) <- paste0("col", 1:new_ncol)
rownames(df_final) <- 1:nrow(df_final)
return(df_final)
}
# Apply the trans_fun
df_final <- trans_fun(df)
df_final
col1 col2
1 1 5
2 2 6
3 3 7
4 4 8
5 5 9
6 6 10
7 7 11
8 8 12
9 9 13
10 10 14
11 11 15
12 12 16
This should do the work: 这应该做的工作:
df2 <- data.frame(col1 = 1:(length(df$step1) + length(df$step2))) df2$col1 <- c(df$step1, df$step2, df$step3) df2$col2 <- c(df$step2, df$step3, df$step4)
Things to point: 要点:
Here is a method using dplyr
and reshape2
- this assumes all of the columns are the same length. 这是使用dplyr
和reshape2
的方法-假定所有列的长度都相同。
library(dplyr)
library(reshape2)
Drop the last column from the dataframe 从数据框中删除最后一列
df[,1:ncol(df)-1]%>%
melt() %>%
dplyr::select(col1=value) -> col1
Drop the first column from the dataframe 从数据框中删除第一列
df %>%
dplyr::select(-step1) %>%
melt() %>%
dplyr::select(col2=value) -> col2
Combine the dataframes 合并数据框
bind_cols(col1, col2)
Are you not just looking to do: 您是否不只是想做:
df2 <- data.frame(col1 = unlist(df[,-nrow(df)]),
col2 = unlist(df[,-1]))
rownames(df2) <- NULL
df2
col1 col2 1 1 5 2 2 6 3 3 7 4 4 8 5 5 9 6 6 10 7 7 11 8 8 12 9 9 13 10 10 14 11 11 15 12 12 16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.