the*_*fly 18 transpose r dataframe
在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
Run Code Online (Sandbox Code Playgroud)
nzc*_*ops 22
那么你可以通过两个步骤使用
# Transpose table YOU WANT
fooData.T <- t(fooData[,2:ncol(fooData)])
# Set the column headings from the first column in the original table
colnames(fooData.T) <- fooData[,1]
Run Code Online (Sandbox Code Playgroud)
结果是你可能知道的矩阵,这是由于转置时的类问题.鉴于转置步骤中缺乏命名能力,我认为没有一种方法可以做到这一点.