ChatGPT解决这个技术问题 Extra ChatGPT

转置数据框

我需要转置一个大数据框,所以我使用了:

df.aree <- t(df.aree)
df.aree <- as.data.frame(df.aree)

这是我得到的:

df.aree[c(1:5),c(1:5)]
                         10428        10760        12148        11865
    name                M231T3       M961T5       M960T6      M231T19
    GS04.A        5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04
    GS16.A        5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04
    GS20.A        5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04
    GS40.A        3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04    

我的问题是我需要消除的新列名(10428、10760、12148、11865),因为我需要使用第一行作为列名。

我尝试了 col.names() 功能,但我没有得到我需要的东西。

你有什么建议吗?

编辑

谢谢你的建议!!!使用它我得到:

df.aree[c(1:5),c(1:5)]
                        M231T3       M961T5       M960T6      M231T19
    GS04.A        5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04
    GS16.A        5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04
    GS20.A        5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04
    GS40.A        3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04
    GS44.A        1.225938e+04 2.681887e+03 1.154924e+04 4.202394e+04

现在我需要在因子列中转换行名(GS..)......

你试过colnames(df.aree)<-df.aree[1,];df.aree<-df.aree[2:nrow(df.aree),]吗?
数据框自然不是可以转置的。如果你的是,那么也许它应该是矩阵形式。
同意; t数据帧的效率也很低。如果可以,请使用矩阵。
转置包含字符串列的 data.frame 会将所有值转换为字符串!不好。请参阅下面的答案以了解解决方法。

T
Tommy

当名称列在其中时,您最好不要转置 data.frame - 所有数值都将转换为字符串!

这是一个将数字保留为数字的解决方案:

# first remember the names
n <- df.aree$name

# transpose all but the first column (name)
df.aree <- as.data.frame(t(df.aree[,-1]))
colnames(df.aree) <- n
df.aree$myfactor <- factor(row.names(df.aree))

str(df.aree) # Check the column types

a
ah bon

您可以使用 data.table 库中的 transpose 函数。将 numeric 值保留为 numeric 的简单快速的解决方案。

library(data.table)

# get data
data("mtcars")

# transpose
t_mtcars <- transpose(mtcars)

# get row and colnames in order
colnames(t_mtcars) <- rownames(mtcars)
rownames(t_mtcars) <- colnames(mtcars)

此外,setnames(t_mtcars, rownames(mtcars)) 将是在 data.table 上设置名称的 data.table 方式(如果使用 data.table 对象,您将不会设置 rownames
这是迄今为止最好的解决方案! +1。
使用 data.table 这样一个雄辩的解决方案,谢谢!
F
Frank
df.aree <- as.data.frame(t(df.aree))
colnames(df.aree) <- df.aree[1, ]
df.aree <- df.aree[-1, ]
df.aree$myfactor <- factor(row.names(df.aree))

@Riccardo 如果是这样,请单击旁边的灰色勾号接受他的回答。
一个问题 - 列名采用因子级别的数字表示。
G
Ghazal

利用 as.matrix

# keep the first column 
names <-  df.aree[,1]

# Transpose everything other than the first column
df.aree.T <- as.data.frame(as.matrix(t(df.aree[,-1])))

# Assign first column as the column names of the transposed dataframe
colnames(df.aree.T) <- names

B
BMLopes

使用 tidyr,可以用“pivot_longer”然后“pivot_wider”转置数据帧。

要转置广泛使用的 mtcars 数据集,您应该首先将行名转换为列(函数 rownames_to_column 创建一个名为“rowname”的新列)。

library(tidyverse)

mtcars %>% 
rownames_to_column() %>% 
pivot_longer(!rowname, names_to = "col1", values_to = "col2") %>% 
pivot_wider(names_from = "rowname", values_from = "col2")

u
user438383

你可以给转置矩阵取另一个名字

df.aree1 <- t(df.aree)
df.aree1 <- as.data.frame(df.aree1)

正如目前所写的那样,您的答案尚不清楚。请edit添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以找到有关如何写出好答案的更多信息in the help center