R:将列变成行(附加列)
我有以下数据集,我想了解如何将列切换为 R 中的行。
Pet Shop | Year | Dog | Fish | Turtle |
---|---|---|---|---|
A | 2021 | 275 | 20 | NA |
A | 2020 | 250 | NA | 50 |
A | 2019 | NA | NA | NA |
B | 2021 | 350 | 75 | NA |
B | 2020 | 北美 | 55 | 85 |
B | 2019 | 北美 | 75 | 北美 |
C | 2021 | 280 | NA | NA |
C | 2020 | NA | NA | NA |
C | 2019 | NA | 60 | NA |
有没有人对如何将列附加为行有任何建议?
宠物店 | 年份 | 动物 | 价格 |
---|---|---|---|
A | 2021 | 狗 | 275 |
A | 2021 | 鱼 | 20 |
A | 2021 | 海龟 | NA |
A | 2020 | 狗 | 250 |
A | 2020 | 鱼 | NA |
A | 2020 | 海龟 | 50 |
A | 2019 | 狗 | NA |
A | 2019 | 鱼 | NA |
A | 2019 | 海龟 | NA |
B | 2021 | 狗 | 350 |
B | 2021 | 鱼 | 75 |
B | 2021 | 海龟 | NA |
B | 2020 | 狗 | NA |
B | 2020 | 鱼 | 55 |
B | 2020 | 海龟 | 85 |
... 及以后
提前致谢!
I've the following dataset and I would like to find out how to switch the column into rows in R.
Pet Shop | Year | dog | fish | turtle |
---|---|---|---|---|
A | 2021 | 275 | 20 | NA |
A | 2020 | 250 | NA | 50 |
A | 2019 | NA | NA | NA |
B | 2021 | 350 | 75 | NA |
B | 2020 | NA | 55 | 85 |
B | 2019 | NA | 75 | NA |
C | 2021 | 280 | NA | NA |
C | 2020 | NA | NA | NA |
C | 2019 | NA | 60 | NA |
Does anyone have any suggestions as to how I can append the columns as rows?
Pet Shop | Year | Animal | Prices |
---|---|---|---|
A | 2021 | dog | 275 |
A | 2021 | fish | 20 |
A | 2021 | turtle | NA |
A | 2020 | dog | 250 |
A | 2020 | fish | NA |
A | 2020 | turtle | 50 |
A | 2019 | dog | NA |
A | 2019 | fish | NA |
A | 2019 | turtle | NA |
B | 2021 | dog | 350 |
B | 2021 | fish | 75 |
B | 2021 | turtle | NA |
B | 2020 | dog | NA |
B | 2020 | fish | 55 |
B | 2020 | turtle | 85 |
... and onwards
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由 reprex 软件包 (v2.0.1) 创建于 2022 年 2 月 25 日
Created on 2022-02-25 by the reprex package (v2.0.1)