在 R 中转置 data.frame 并将其中一列设置为新转置表的标题的最佳方法是什么?我在下面编写了一种方法来做到这一点。由于我还是 R 的新手。我想要改进我的代码的建议以及更像 R 的替代方案。不幸的是,我的解决方案也有点硬编码(即新的列标题位于某个位置)。
# Assume a data.frame called fooData
# Assume the column is the first column before transposing
# Transpose table
fooData.T <- t(fooData)
# Set the column headings
colnames(fooData.T) <- test[1,]
# Get rid of the column heading row
fooData.T <- fooData.T[2:nrow(fooData.T), ]
#fooData.T now contains a transposed table with the first column as headings