英文:
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 |>
rownames_to_column(var = "car") |>
select(1:3) |>
pivot_wider(names_from = car, values_from = mpg)
Result
# A 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>
答案2
得分: 0
我认为你要找的关键词是“列联表”,一种(稀疏)矩阵形式。
你可以使用xtabs和公式语法。公式的第一个参数mpg
将出现在单元格中,并由cyl
和包含每辆汽车名称的rownames(mtcars)
解释。xtabs的第二个参数是数据框本身。
xtabs(mpg~cyl+rownames(mtcars),mtcars)|>
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)|>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论