我如何将Exsist列用作样本ID

发布于 2025-01-31 03:28:03 字数 572 浏览 1 评论 0原文

我将Excel文件进​​口了一个看起来像这样的动物数据:

AnimalId体重
AB113.454
AB212.984
AB323.225
AB423.015

我想用我的第一列(Animaid)用作Rownames。

我已经搜索了一个答案,但是大多数问题介绍了新列的创建,该列是Rownames列。我实际上感到愚蠢地来到这里问这个。我的目的是使用GGPLOT2创建PCA图,这要求我将数据框架的格式以GGPLOTS2识别的格式。

I imported an excel file with animal data that looks something like this:

AnimalIDSexBody WeightDistrict
AB113.454
AB212.984
AB323.225
AB423.015

I want to use to use my first column(AnimaID) as rownames.

I've searched for an answer but most questions address the creation of a new column that then becomes the rownames column. I actually feel stupid coming on here and asking this. My aim is to create a pca plot using ggplot2, and that requires me to have the dataframe in the format that ggplots2 recognizes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2025-02-07 03:28:03

可能的解决方案,在基本r中:

rownames(df) <- df$AnimalID
df <- df[,-1]
df

#>     Sex Body.Weight District
#> AB1   1        3.45        4
#> AB2   1        2.98        4
#> AB3   2        3.22        5
#> AB4   2        3.01        5

或使用tidyverse

library(tidyverse)

df %>% column_to_rownames("AnimalID")

#>     Sex Body.Weight District
#> AB1   1        3.45        4
#> AB2   1        2.98        4
#> AB3   2        3.22        5
#> AB4   2        3.01        5

A possible solution, in base R:

rownames(df) <- df$AnimalID
df <- df[,-1]
df

#>     Sex Body.Weight District
#> AB1   1        3.45        4
#> AB2   1        2.98        4
#> AB3   2        3.22        5
#> AB4   2        3.01        5

Or using tidyverse:

library(tidyverse)

df %>% column_to_rownames("AnimalID")

#>     Sex Body.Weight District
#> AB1   1        3.45        4
#> AB2   1        2.98        4
#> AB3   2        3.22        5
#> AB4   2        3.01        5
弄潮 2025-02-07 03:28:03

另一个base选项:

row.names(df) <- df$AnimalID
df[1] <- NULL

输出:

    Sex Body_Weight District
AB1   1        3.45        4
AB2   1        2.98        4
AB3   2        3.22        5
AB4   2        3.01        6

Another base option:

row.names(df) <- df$AnimalID
df[1] <- NULL

Output:

    Sex Body_Weight District
AB1   1        3.45        4
AB2   1        2.98        4
AB3   2        3.22        5
AB4   2        3.01        6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文