Wrap a 2 Column Data Frame in R

烈酒焚心 提交于 2021-02-08 06:57:23

问题


Let's say I have a data frame with x values (10 in this example), and 2 columns. Is it possible to print that data frame and have it wrap its output across a desired amount of rows, rather than have it print as just x rows? An example below with 10 values:

Current output:

  V1          V2
   1   -0.54850033
   2   -0.41569523
   3    1.25346656
   4    2.08200119
   5    1.18916344
   .    ..........
   10  0.18345154

Desired output:
      V1          V2       V1          V2
       1   -0.54850033      6   -0.45362345
       2   -0.41569523      7    1.23466542
       3    1.25346656      8    2.98907097
       4    2.08200119      9    0.53153214
       5    1.18916344     10    1.43453377

Is there an option in print() that can be used for this?


回答1:


A general solution (similar to @d.b's in comments) with dplyr + tidyr. This extends to an arbitrary number of columns (as long as you have an id column) and an arbitrary number of resulting rows:

library(dplyr)
library(tidyr)

split_df = function(DF, nrows = ceiling(nrow(DF)/2)){
  DF %>%
    mutate(id = rep(1:nrow(.), each = nrows, len = nrow(.))) %>%
    gather(variable, value, -id) %>%
    unite(temp, id, variable) %>%
    group_by(temp) %>%
    mutate(id = 1:n()) %>%
    spread(temp, value) %>%
    select(-id) %>%
    data.frame() %>%
    setNames(rep(names(DF), ceiling(nrow(DF)/nrows))) 
}

Results:

> split_df(df)
  V1          V2         V3 V1         V2         V3
1  1 -0.56047565  1.2240818  6  1.7150650  1.7869131
2  2 -0.23017749  0.3598138  7  0.4609162  0.4978505
3  3  1.55870831  0.4007715  8 -1.2650612 -1.9666172
4  4  0.07050839  0.1106827  9 -0.6868529  0.7013559
5  5  0.12928774 -0.5558411 10 -0.4456620 -0.4727914

> split_df(df, 4)
  V1          V2        V3 V1         V2         V3 V1         V2         V3
1  1 -0.56047565 1.2240818  5  0.1292877 -0.5558411  9 -0.6868529  0.7013559
2  2 -0.23017749 0.3598138  6  1.7150650  1.7869131 10 -0.4456620 -0.4727914
3  3  1.55870831 0.4007715  7  0.4609162  0.4978505 NA         NA         NA
4  4  0.07050839 0.1106827  8 -1.2650612 -1.9666172 NA         NA         NA

> split_df(df, 6)
  V1          V2         V3 V1         V2         V3
1  1 -0.56047565  1.2240818  7  0.4609162  0.4978505
2  2 -0.23017749  0.3598138  8 -1.2650612 -1.9666172
3  3  1.55870831  0.4007715  9 -0.6868529  0.7013559
4  4  0.07050839  0.1106827 10 -0.4456620 -0.4727914
5  5  0.12928774 -0.5558411 NA         NA         NA
6  6  1.71506499  1.7869131 NA         NA         NA

Data:

set.seed(123)
df = data.frame(V1 = 1:10, V2 = rnorm(10), V3 = rnorm(10))



回答2:


If you just want to print half of the rows of a data frame alongside the second half, you could try something using cbind():

df2 <- cbind(df[1:(nrow(df)/2), ], df[(1+(nrow(df)/2)):nrow(df), ])
print(df2)


来源:https://stackoverflow.com/questions/46380954/wrap-a-2-column-data-frame-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!