0
這裏是樣本數據:如何data.frame拆分等於列
df <- data.frame(t(data.frame(seq(1,10,1)))); rownames(df) <- NULL;
colnames(df) <- letters[1:ncol(df)]
df
我想安排新data.frame,使其始終具有6
列,下一行(因爲夾板的NcoI後> 6)將包含接下來的6列名稱和下一行的值。最後一行if ncol < 6這些值用包含列名的空字符串填充。
在這裏期望的輸出:
a b c d e f
1 1 2 3 4 5 6
2 g h i j
3 7 8 9 10
又如:
df <- data.frame(t(data.frame(seq(1,15,1)))); rownames(df) <- NULL;
colnames(df) <- letters[1:ncol(df)]
df
a b c d e f
1 1 2 3 4 5 6
2 g h i j k l
3 7 8 9 10 11 12
4 m n o
5 13 14 15
編輯:
到可能接近它的方法是:
n <- 6
ncl <- nrow(df)
s <- split(df, rep(1:ceiling(ncl/n), each=n, length.out=ncl))
s
s1 <- split(rownames(df), rep(1:ceiling(ncl/n), each=n, length.out=ncl))
s1
結合每秒的s
PLIT和s1
s1[c(TRUE,FALSE)]
它適用於該示例,但不適用於實際數據。這個解決方案對於這個任務來說太複雜了(醜陋的黑客攻擊),這讓我想到如何使它適應需求。你也需要'jsonlite'包來使它工作,並且'rbind.pages'不被棄用,而是使用'rbind_pages'。謝謝。 – Maximilian
這沒有給出所需的輸出 –
編輯...複製在錯誤的行。道歉... –