English 中文(简体)
如何将表格转换为数据框架
原标题:How to convert a table to a data frame
  • 时间:2012-05-25 17:35:18
  •  标签:
  • r
  • dataframe

我有一个 R 中的表格,该表格有 str () :

 table [1:3, 1:4] 0.166 0.319 0.457 0.261 0.248 ...
 - attr(*, "dimnames")=List of 2
  ..$ x: chr [1:3] "Metro >=1 million" "Metro <1 million" "Non-Metro Counties"
  ..$ y: chr [1:4] "q1" "q2" "q3" "q4"

当我打印的时候,我看起来是这样的:

                    y
x                           q1        q2        q3        q4
  Metro >=1 million  0.1663567 0.2612212 0.2670441 0.3053781
  Metro <1 million   0.3192857 0.2480012 0.2341030 0.1986102
  Non-Metro Counties 0.4570341 0.2044960 0.2121102 0.1263597

我想摆脱 x y ,并将其转换为与以上完全相同的数据框架(三行,四列),但不使用 >x y 。 如果我使用 as.data.frame(mytable) ,则我理解:

                    x  y      Freq
1   Metro >=1 million q1 0.1663567
2    Metro <1 million q1 0.3192857
3  Non-Metro Counties q1 0.4570341
4   Metro >=1 million q2 0.2612212
5    Metro <1 million q2 0.2480012
6  Non-Metro Counties q2 0.2044960
7   Metro >=1 million q3 0.2670441
8    Metro <1 million q3 0.2341030
9  Non-Metro Counties q3 0.2121102
10  Metro >=1 million q4 0.3053781
11   Metro <1 million q4 0.1986102
12 Non-Metro Counties q4 0.1263597

我可能根本上无法理解表格与数据框架的关系。

最佳回答

我已经想通了

as.data.frame.matrix(mytable) 

做我需要做的 -- 显然,表格需要以某种方式转换为矩阵, 才能被适当转换为数据框架。 我发现这个 < a href=" http://todjobe.blogspot.com/2010/08/converting-r- continence- continente- tables-to- data.html" rel=“ noreferrer” as. data. frame.matrix () 功能, 用于计算生态学博客 的应急表格。

问题回答

虽然由于列名是数字,结果在此情况下有所不同,但我使用的另一种方式是 data.frame(marband(mytable)) 。 使用来自 @ X. X 的示例:

> freq_t = table(cyl = mtcars$cyl, gear = mtcars$gear)

> freq_t
   gear
cyl  3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2

> data.frame(rbind(freq_t))
  X3 X4 X5
4  1  8  2
6  2  4  1
8 12  0  2

如果列名不以数字开始, X 就不会被添加到前面。

简短回答:使用 as.data.frame.matrix( Mytable) ,正如@Victor Van Hee 所建议的那样。

长答案 : as. data. frame(mytable) 可能无法在 table () 函数生成的应急表格上工作,即使 is.matrix(your_table) 返回 TRUE 。 它仍然会将您表格熔化为 factor1 cabli 计数 格式。

示例:

> freq_t = table(cyl = mtcars$cyl, gear = mtcars$gear)

> freq_t
   gear
cyl  3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2

> is.matrix(freq_t)
[1] TRUE

> as.data.frame(freq_t)
  cyl gear Freq
1   4    3    1
2   6    3    2
3   8    3   12
4   4    4    8
5   6    4    4
6   8    4    0
7   4    5    2
8   6    5    1
9   8    5    2
> as.data.frame.matrix(freq_t)
   3 4 5
4  1 8 2
6  2 4 1
8 12 0 2

如果您使用 < a href=> https://blog.rstudio.org/2016/09/15/tidyverse-1-0-0/" rel=" noreferrer" >tidyverse , 您可以使用

as_data_frame(table(myvector))

获取 < a href=> "https://blog.rstudio.org/2016/03/24/tble-1-0-0/" rel="noreferrer">tble (即数据框架,与基准等级略有不同)





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签