R:用数据框的两个行的名字组成矩阵中的列名

huwehgph  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(137)

我有这样的数据

  1. District Year Social.Assistance Danger.Poverty GINI S80S20
  2. Charlottenburg-Wilmersdorf 2011 14.6 12.2 0.33 5.1
  3. Friedrichshain-Kreuzberg 2011 25.8 21.3 0.30 4.3
  4. Lichtenberg 2011 20.8 11.9 0.23 3.1
  5. Marzahn-Hellersdorf 2011 22.1 15.6 0.27 3.8
  6. Mitte 2011 27.9 18.6 0.29 4.1
  7. Neukoelln 2011 29.0 22.5 0.27 3.6
  8. Pankow 2011 13.7 10.4 0.26 3.7
  9. Reinickendorf 2011 19.0 12.5 0.29 4.3

我想将数据框转换为一个矩阵,说明Social.Assistance、Danger.Poverty、GINI和S80S20,并以District和Year作为行的名称:

  1. row_names Social.Assistance Danger.Poverty GINI S80S20
  2. Charlottenburg-Wilmersdorf 2011 14.6 12.2 0.33 5.1
  3. Friedrichshain-Kreuzberg 2011 25.8 21.3 0.30 4.3
  4. Lichtenberg 2011 20.8 11.9 0.23 3.1
  5. Marzahn-Hellersdorf 2011 22.1 15.6 0.27 3.8
  6. Mitte 2011 27.9 18.6 0.29 4.1
  7. Neukoelln 2011 29.0 22.5 0.27 3.6
  8. Pankow 2011 13.7 10.4 0.26 3.7
  9. Reinickendorf 2011 19.0 12.5 0.29 4.3

我想有一个简单的方法,我只是没有看到。

tv6aics1

tv6aics11#

  1. rownames(d) <- paste(d$District, d$Year)
  2. d$District <- NULL
  3. d$Year <- NULL

相关问题