更改列与行

huangapple go评论48阅读模式
英文:

Changing columns with rows

问题

我不确定如何适当地提出这个问题,这可能是我无法解决它的原因。

所以基本上,当使用mtcars数据集时,我只选择3列,比如车名、mpg和cyl。现在我想要一个数据框,在这个数据框中,我将在Y轴上拥有cyl的数量。

基本上,我将在x轴上拥有车名,然后在y轴上,我将拥有cyl编号,比如4/6/8,最后在单元格内是mpg。

重新排列数据框或其他什么,但我找不到任何有用的东西,请指点我正确的方向,强大的stackoverflow!

英文:

I'm not sure how to ask this question properly and that's probably why I can't figure it out.

So basically when using mtcars dataset I pick only 3 columns let's say the name of the car, mpg, and cyl. So now I want to have a data frame where I will have the number of cyl. on the Y axis.

Basically, I will have the name of the car on the x-axis, then in the y-axis I will have cyl nr like 4/6/8, and finally inside the cells the mpg.

Rearranging the data frame or something, but I can't find anything helpful, please point me in the right direction mighty stackoverflow!

答案1

得分: 1

也许是这样?

library(tidyverse)
mtcars |>
  rownames_to_column(var = "car") |>
  select(1:3) |>
  pivot_wider(names_from = car, values_from = mpg)

结果

# 一个 tibble: 3 × 33
    cyl `Mazda RX4` `Mazda RX4 Wag` `Datsun 710` `Hornet 4 Drive` `Hornet Sportabout` Valiant `Duster 360` `Merc 240D` `Merc 230` `Merc 280`
  <dbl>       <dbl>           <dbl>        <dbl>            <dbl>               <dbl>   <dbl>        <dbl>       <dbl>      <dbl>      <dbl>
1     6          21              21         NA               21.4                NA      18.1         NA          NA         NA         19.2
2     4          NA              NA         22.8             NA                  NA      NA           NA          24.4       22.8       NA  
3     8          NA              NA         NA               NA                  18.7    NA           14.3        NA         NA         NA  
# ℹ 22 more variables: `Merc 280C` <dbl>, `Merc 450SE` <dbl>, `Merc 450SL` <dbl>, `Merc 450SLC` <dbl>, `Cadillac Fleetwood` <dbl>,
#   `Lincoln Continental` <dbl>, `Chrysler Imperial` <dbl>, `Fiat 128` <dbl>, `Honda Civic` <dbl>, `Toyota Corolla` <dbl>,
#   `Toyota Corona` <dbl>, `Dodge Challenger` <dbl>, `AMC Javelin` <dbl>, `Camaro Z28` <dbl>, `Pontiac Firebird` <dbl>, `Fiat X1-9` <dbl>,
#   `Porsche 914-2` <dbl>, `Lotus Europa` <dbl>, `Ford Pantera L` <dbl>, `Ferrari Dino` <dbl>, `Maserati Bora` <dbl>, `Volvo 142E` <dbl>
英文:

Maybe this?

library(tidyverse)
mtcars |&gt;
  rownames_to_column(var = &quot;car&quot;) |&gt;
  select(1:3) |&gt;
  pivot_wider(names_from = car, values_from = mpg)

Result

# A tibble: 3 &#215; 33
    cyl `Mazda RX4` `Mazda RX4 Wag` `Datsun 710` `Hornet 4 Drive` `Hornet Sportabout` Valiant `Duster 360` `Merc 240D` `Merc 230` `Merc 280`
  &lt;dbl&gt;       &lt;dbl&gt;           &lt;dbl&gt;        &lt;dbl&gt;            &lt;dbl&gt;               &lt;dbl&gt;   &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;
1     6          21              21         NA               21.4                NA      18.1         NA          NA         NA         19.2
2     4          NA              NA         22.8             NA                  NA      NA           NA          24.4       22.8       NA  
3     8          NA              NA         NA               NA                  18.7    NA           14.3        NA         NA         NA  
# ℹ 22 more variables: `Merc 280C` &lt;dbl&gt;, `Merc 450SE` &lt;dbl&gt;, `Merc 450SL` &lt;dbl&gt;, `Merc 450SLC` &lt;dbl&gt;, `Cadillac Fleetwood` &lt;dbl&gt;,
#   `Lincoln Continental` &lt;dbl&gt;, `Chrysler Imperial` &lt;dbl&gt;, `Fiat 128` &lt;dbl&gt;, `Honda Civic` &lt;dbl&gt;, `Toyota Corolla` &lt;dbl&gt;,
#   `Toyota Corona` &lt;dbl&gt;, `Dodge Challenger` &lt;dbl&gt;, `AMC Javelin` &lt;dbl&gt;, `Camaro Z28` &lt;dbl&gt;, `Pontiac Firebird` &lt;dbl&gt;, `Fiat X1-9` &lt;dbl&gt;,
#   `Porsche 914-2` &lt;dbl&gt;, `Lotus Europa` &lt;dbl&gt;, `Ford Pantera L` &lt;dbl&gt;, `Ferrari Dino` &lt;dbl&gt;, `Maserati Bora` &lt;dbl&gt;, `Volvo 142E` &lt;dbl&gt;

答案2

得分: 0

我认为你要找的关键词是“列联表”,一种(稀疏)矩阵形式。
你可以使用xtabs和公式语法。公式的第一个参数mpg将出现在单元格中,并由cyl和包含每辆汽车名称的rownames(mtcars)解释。xtabs的第二个参数是数据框本身。

xtabs(mpg~cyl+rownames(mtcars),mtcars)|&gt;
 as.data.frame.matrix()

在使用xtabs后,使用as.dataframe.matrix将其保持为矩阵形式,否则它将被转换为长表格。

英文:

I think the keyword you are looking for is contingency table a form of (sparse)-matrix.
You can use xtabs and formula syntax. The frist argument of the formula mpg will be in the cells and will be explained by cyl and the rownames(mtcars) containing the names of each car. The second argument of xtabs is the dataframe itself.

xtabs(mpg~cyl+rownames(mtcars),mtcars)|&gt;
 as.data.frame.matrix()

After xtabs use as.dataframe.matrix to keep it in the matrix form, otherwise it will be converted to a long table.

huangapple
  • 本文由 发表于 2023年7月18日 02:28:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定