我有一个名为df_high_A的数据框,如此
C1 C2
02-01-07 10 23
03-01-07 20 12
04-01-07 14 17
想要像这样做
C1 C2 C3
02-01-07 10 23
03-01-07 20 12
04-01-07 14 17
当我使用此代码时
library (tibble)
df_high_A<-tibble::rowid_to_column(data.frame(df_high_A),"date")
看起来像这样
C1 C2 C3
1 10 23
2 20 12
3 14 17
解决方案是什么?
答案 0 :(得分:0)
假设您的数据是
dput( df_high_A )
structure(list(C1 = c(10L, 20L, 14L), C2 = c(23L, 12L, 17L)), .Names = c("C1",
"C2"), class = "data.frame", row.names = c("02-01-07", "03-01-07",
"04-01-07"))
使用Base R,解决方案将是
df_high_A <- data.frame( "C1" = row.names( df_high_A ),
"C2" = df_high_A$C1,
"C3" = df_high_A$C2 )
产生
df_high_A
C1 C2 C3
1 02-01-07 10 23
2 03-01-07 20 12
3 04-01-07 14 17
答案 1 :(得分:0)
如果你的rownames总是可以尝试将它们绑定在一起的日期,那么重置列名和行名。
df_high_A = cbind(rownames(df_high_A), df_high_A)
colnames(df_high_A) = paste("C",1:ncol(df_high_A),sep="")
rownames(df_high_A) = 1:nrow(df_high_A)