R:将列变成行(附加列)

发布于 2025-01-10 09:29:56 字数 1991 浏览 0 评论 0原文

我有以下数据集,我想了解如何将列切换为 R 中的行。

Pet ShopYearDogFishTurtle
A202127520NA
A2020250NA50
A2019NANANA
B202135075NA
B2020北美5585
B2019北美75北美
C2021280NANA
C2020NANANA
C2019NA60NA

有没有人对如何将列附加为行有任何建议?

宠物店年份动物价格
A2021275
A202120
A2021海龟NA
A2020250
A2020NA
A2020海龟50
A2019NA
A2019NA
A2019海龟NA
B2021350
B202175
B2021海龟NA
B2020NA
B202055
B2020海龟85

... 及以后

提前致谢!

I've the following dataset and I would like to find out how to switch the column into rows in R.

Pet ShopYeardogfishturtle
A202127520NA
A2020250NA50
A2019NANANA
B202135075NA
B2020NA5585
B2019NA75NA
C2021280NANA
C2020NANANA
C2019NA60NA

Does anyone have any suggestions as to how I can append the columns as rows?

Pet ShopYearAnimalPrices
A2021dog275
A2021fish20
A2021turtleNA
A2020dog250
A2020fishNA
A2020turtle50
A2019dogNA
A2019fishNA
A2019turtleNA
B2021dog350
B2021fish75
B2021turtleNA
B2020dogNA
B2020fish55
B2020turtle85

... and onwards

Thanks in advance!

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

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

发布评论

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

评论(1

梦亿 2025-01-17 09:29:56
library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
          Pet.Shop = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
              Year = c(2021L,2020L,2019L,2021L,
                       2020L,2019L,2021L,2020L,2019L),
               dog = c(275L, 250L, NA, 350L, NA, NA, 280L, NA, NA),
              fish = c(20L, NA, NA, 75L, 55L, 75L, NA, NA, 60L),
            turtle = c(NA, 50L, NA, NA, 85L, NA, NA, NA, NA)
)

df %>% 
  as_tibble() %>% 
  pivot_longer(cols = dog:turtle,
               names_to = "Animal",
               values_to = "Prices")

#> # A tibble: 27 x 4
#>    Pet.Shop  Year Animal Prices
#>    <chr>    <int> <chr>   <int>
#>  1 A         2021 dog       275
#>  2 A         2021 fish       20
#>  3 A         2021 turtle     NA
#>  4 A         2020 dog       250
#>  5 A         2020 fish       NA
#>  6 A         2020 turtle     50
#>  7 A         2019 dog        NA
#>  8 A         2019 fish       NA
#>  9 A         2019 turtle     NA
#> 10 B         2021 dog       350
#> # ... with 17 more rows

reprex 软件包 (v2.0.1) 创建于 2022 年 2 月 25 日

library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
          Pet.Shop = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
              Year = c(2021L,2020L,2019L,2021L,
                       2020L,2019L,2021L,2020L,2019L),
               dog = c(275L, 250L, NA, 350L, NA, NA, 280L, NA, NA),
              fish = c(20L, NA, NA, 75L, 55L, 75L, NA, NA, 60L),
            turtle = c(NA, 50L, NA, NA, 85L, NA, NA, NA, NA)
)

df %>% 
  as_tibble() %>% 
  pivot_longer(cols = dog:turtle,
               names_to = "Animal",
               values_to = "Prices")

#> # A tibble: 27 x 4
#>    Pet.Shop  Year Animal Prices
#>    <chr>    <int> <chr>   <int>
#>  1 A         2021 dog       275
#>  2 A         2021 fish       20
#>  3 A         2021 turtle     NA
#>  4 A         2020 dog       250
#>  5 A         2020 fish       NA
#>  6 A         2020 turtle     50
#>  7 A         2019 dog        NA
#>  8 A         2019 fish       NA
#>  9 A         2019 turtle     NA
#> 10 B         2021 dog       350
#> # ... with 17 more rows

Created on 2022-02-25 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文