如何将一列的数据框架分为R中的两个列?

发布于 2025-02-13 12:53:56 字数 1208 浏览 0 评论 0原文

如何将一列的数据框架分为R中的两个列?

这是dput(head(分类法,n = 30))

structure(list(X1..Neodiprion.virginianus = c("    species, hymenopterans", 
"2. Nepsalus jezoensis", "    species, insects", "3. Prochas sp. 2 YYH-2022a", 
"    species, wasps, ants & bees", "4. Prochas sp. 1 YYH-2022a", 
"    species, wasps, ants & bees", "5. Eccoptopterus sp. 1 CP-2022", 
"    species, beetles", "6. Andricus sp. 1 CYS-2022a", "    species, wasps, ants & bees", 
"7. Paralabellula curvicauda", "    species, earwigs", "8. Paralabellula", 
"    genus, earwigs", "9. Pristiphora sp.", "    species, hymenopterans", 
"10. Phyllotreta flexuosa", "    species, beetles", "11. Nematinae sp.", 
"    species, hymenopterans", "12. Euura sp.", "    species, hymenopterans", 
"13. Dolerus sp.", "    species, hymenopterans", "14. Ivieolus inflaticollis", 
"    species, beetles", "15. Germarostes sp.", "    species, beetles", 
"16. Germarostes globosus")), row.names = c(NA, 30L), class = "data.frame")

我需要循环数据框架: [![输入图像描述在此处] [1]] [1]

非常感谢! [1]: https://i.sstatic.net/ly1ho.png

How can I separate the data frame of one column into two columns in R?

This the dput(head(taxonomy, n=30))

structure(list(X1..Neodiprion.virginianus = c("    species, hymenopterans", 
"2. Nepsalus jezoensis", "    species, insects", "3. Prochas sp. 2 YYH-2022a", 
"    species, wasps, ants & bees", "4. Prochas sp. 1 YYH-2022a", 
"    species, wasps, ants & bees", "5. Eccoptopterus sp. 1 CP-2022", 
"    species, beetles", "6. Andricus sp. 1 CYS-2022a", "    species, wasps, ants & bees", 
"7. Paralabellula curvicauda", "    species, earwigs", "8. Paralabellula", 
"    genus, earwigs", "9. Pristiphora sp.", "    species, hymenopterans", 
"10. Phyllotreta flexuosa", "    species, beetles", "11. Nematinae sp.", 
"    species, hymenopterans", "12. Euura sp.", "    species, hymenopterans", 
"13. Dolerus sp.", "    species, hymenopterans", "14. Ivieolus inflaticollis", 
"    species, beetles", "15. Germarostes sp.", "    species, beetles", 
"16. Germarostes globosus")), row.names = c(NA, 30L), class = "data.frame")

I need the folowing data frame:
[![enter image description here][1]][1]

Many thanks!
[1]: https://i.sstatic.net/ly1ho.png

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

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

发布评论

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

评论(1

可遇━不可求 2025-02-20 12:53:56

您可以使用以下事实,即排回收索引,因此c(t,f)将从第一行开始其他行,c(f,t)将获得从第二行开始的所有其他行:

data.frame(x = df[c(F,T),], y = trimws(df[c(T,F),]))

输出

                                x                           y
1           2. Nepsalus jezoensis      species, hymenopterans
2      3. Prochas sp. 2 YYH-2022a            species, insects
3      4. Prochas sp. 1 YYH-2022a species, wasps, ants & bees
4  5. Eccoptopterus sp. 1 CP-2022 species, wasps, ants & bees
5     6. Andricus sp. 1 CYS-2022a            species, beetles
6     7. Paralabellula curvicauda species, wasps, ants & bees
7                8. Paralabellula            species, earwigs
8              9. Pristiphora sp.              genus, earwigs
9        10. Phyllotreta flexuosa      species, hymenopterans
10              11. Nematinae sp.            species, beetles
11                  12. Euura sp.      species, hymenopterans
12                13. Dolerus sp.      species, hymenopterans
13     14. Ivieolus inflaticollis      species, hymenopterans
14            15. Germarostes sp.            species, beetles
15       16. Germarostes globosus            species, beetles

You can use the fact that row indices are recycled so c(T,F) will get every other row starting with the first row and c(F,T) will get every other row starting with the second row:

data.frame(x = df[c(F,T),], y = trimws(df[c(T,F),]))

Output

                                x                           y
1           2. Nepsalus jezoensis      species, hymenopterans
2      3. Prochas sp. 2 YYH-2022a            species, insects
3      4. Prochas sp. 1 YYH-2022a species, wasps, ants & bees
4  5. Eccoptopterus sp. 1 CP-2022 species, wasps, ants & bees
5     6. Andricus sp. 1 CYS-2022a            species, beetles
6     7. Paralabellula curvicauda species, wasps, ants & bees
7                8. Paralabellula            species, earwigs
8              9. Pristiphora sp.              genus, earwigs
9        10. Phyllotreta flexuosa      species, hymenopterans
10              11. Nematinae sp.            species, beetles
11                  12. Euura sp.      species, hymenopterans
12                13. Dolerus sp.      species, hymenopterans
13     14. Ivieolus inflaticollis      species, hymenopterans
14            15. Germarostes sp.            species, beetles
15       16. Germarostes globosus            species, beetles
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文