将long tibble转换为多列HTML表

问题描述 投票:0回答:1

我有一个包含许多行的双列tibble,我想在HTML表中显示tibble的内容,同时将tibble的内容流向多列。这是我正在使用的典型数据样本。

structure(list(scores = 328:360, points = c(1.44976648822324, 
2.39850620178477, 3.54432361637504, 4.87641377160755, 6.38641933285773, 
8.06758106817846, 9.91425882252425, 11.9216360940354, 14.0855256867808, 
16.4022354393545, 18.8684718158155, 21.4812684932283, 24.2379320869751, 
27.136, 30.1732070790481, 33.3474588150326, 36.6568095024477, 
40.0994442217347, 43.6736638137649, 47.3778722279125, 51.2105657758874, 
55.1703239324027, 59.2558014037444, 63.46572124494, 67.7988688512606, 
72.2540866842331, 76.8302696189673, 81.5263608204015, 86.3413480724772, 
91.2742604972992, 96.3241656118041, 101.490166677918, 106.771400309065
)), row.names = c(NA, -33L), class = c("tbl_df", "tbl", "data.frame"
))

我想在HTML表格中显示这些数据对,如下所示:

| Score | Points | Score | Points | Score | Points | Score | Points |
|------:|-------:|------:|-------:|------:|-------:|------:|-------:|
|   328 |    1.4 |   337 |   16.4 |   346 |   43.7 |   355 |   81.5 |
|   329 |    2.4 |   338 |   18.9 |   347 |   47.4 |   356 |   86.3 |
|   330 |    3.5 |   339 |   21.5 |   348 |   51.2 |   357 |   91.3 |
|   331 |    4.9 |   340 |   24.2 |   349 |   55.2 |   358 |   96.3 |
|   332 |    6.4 |   341 |   27.1 |   350 |   59.3 |   359 |  101.5 |
|   333 |    8.1 |   342 |   30.2 |   351 |   63.5 |   360 |  106.8 |
|   334 |    9.9 |   343 |   33.3 |   352 |   67.8 |       |        |
|   335 |   11.9 |   344 |   36.7 |   353 |   72.3 |       |        |
|   336 |   14.1 |   345 |   40.1 |   354 |   76.8 |       |        |

我希望有一个解决方案,无论原始tibble中有多少行,都会生成四列双列布局。

我开始将切片分成四个部分,但因为第四部分没有前三部分那么多而被卡住了。

有关完成此任务的方法的任何建议?

r
1个回答
0
投票

您可以编写一个小函数,它将接收您需要的数据和列数。默认只有4列

reshaping = function(dat, cols = 4){
  n = nrow(dat)
  m = ceiling(n/cols)
  time=rep(1:cols, each = m, len = n)
  id = rep(1:m, times = cols, len = n)
  reshape(cbind(id, time, dat), idvar = 'id', dir='wide')[-1]
}

reshaping(dat)
  scores.1  points.1 scores.2 points.2 scores.3 points.3 scores.4  points.4
1      328  1.449766      337 16.40224      346 43.67366      355  81.52636
2      329  2.398506      338 18.86847      347 47.37787      356  86.34135
3      330  3.544324      339 21.48127      348 51.21057      357  91.27426
4      331  4.876414      340 24.23793      349 55.17032      358  96.32417
5      332  6.386419      341 27.13600      350 59.25580      359 101.49017
6      333  8.067581      342 30.17321      351 63.46572      360 106.77140
7      334  9.914259      343 33.34746      352 67.79887       NA        NA
8      335 11.921636      344 36.65681      353 72.25409       NA        NA
9      336 14.085526      345 40.09944      354 76.83027       NA        NA

reshaping(dat,8)
  scores.1 points.1 scores.2  points.2 scores.3 points.3 scores.4 points.4 scores.5 points.5 scores.6 points.6 scores.7  points.7
1      328 1.449766      333  8.067581      338 18.86847      343 33.34746      348 51.21057      353 72.25409      358  96.32417
2      329 2.398506      334  9.914259      339 21.48127      344 36.65681      349 55.17032      354 76.83027      359 101.49017
3      330 3.544324      335 11.921636      340 24.23793      345 40.09944      350 59.25580      355 81.52636      360 106.77140
4      331 4.876414      336 14.085526      341 27.13600      346 43.67366      351 63.46572      356 86.34135       NA        NA
5      332 6.386419      337 16.402235      342 30.17321      347 47.37787      352 67.79887      357 91.27426       NA        NA
© www.soinside.com 2019 - 2024. All rights reserved.