重新排序R中的列

时间:2018-12-14 18:54:44

标签: r tidyr

我正在使用summarise_all函数来尝试为30个不同的变量创建表。

function cleanArray(arr, whitelist) {
  // remove all non-whitelisted keys from the array
}

let origArr = [
  { keep1: 'abc', keep2: 'def', buh1: false, buh2: false },
  { keep3: 'abc', keep4: 'def', buh3: false, buh4: true },
  { keep5: 'abc', keep6: 'def', buh5: false, buh5: false }
];

let whiteList = ['keep1', 'keep2', 'keep3', 'keep4', 'keep5'];
let resultArr = cleanArray(origArr, whiteList);

// result should look like this
resultArr = [
  { keep1: 'abc', keep2: 'def' },
  { keep3: 'abc', keep4: 'def' },
  { keep5: 'abc', keep6: 'def' } 
];

第一部分代码按预期工作。现在,我试图将数据组织到类似于excel的表中,但是当我手动检查最大值,最小值等时,它们的值不正确。

df <- RATIOS

df.sum <- df %>% 
  select(X1Y, X2Y, X3Y, X4Y, X5Y) %>%
  summarise_all(funs(min = min, 
                     max = max,
                     mean = mean,
                     first = first))

任何帮助将不胜感激!!!

df.stats.tidy <-
  df.sum %>% 
  gather(stat, val) %>%
  separate(stat, into = c("var", "stat"), sep = "_") %>%
  spread(stat, val) %>%
  select(min, max, mean, first) # reorder columns

print(df.stats.tidy)

1 个答案:

答案 0 :(得分:0)

您的代码似乎可以为我工作?首先,一些玩具数据:

xarray
library("tidyverse")

# Make some toy data
set.seed(1)
n <- 5
df <- tibble("X1Y" = runif(n), "X2Y" = runif(n), "X3Y" = runif(n), 
             "X4Y" = runif(n), "X5Y" = runif(n))

print(df)

现在,我们运行您的代码:

 ## A tibble: 5 x 5
#    X1Y    X2Y   X3Y   X4Y   X5Y
#   <dbl>  <dbl> <dbl> <dbl> <dbl>
#1 0.266 0.898  0.206 0.498 0.935
#2 0.372 0.945  0.177 0.718 0.212
#3 0.573 0.661  0.687 0.992 0.652
#4 0.908 0.629  0.384 0.380 0.126
#5 0.202 0.0618 0.770 0.777 0.267
df.sum <- df %>% 
  select(X1Y, X2Y, X3Y, X4Y, X5Y) %>%
  summarise_all(funs(min = min, 
                 max = max,
                 mean = mean,
                 first = first))

df.stats.tidy <- 
  df.sum %>% 
  gather(stat, val) %>%
  separate(stat, into = c("var", "stat"), sep = "_") %>%
  spread(stat, val) %>%
  select(var, min, max, mean, first)  # Keeping "var" here 

print(df.stats.tidy)

例如,X4Y的最大值为0.992,与上面的目视检查一致。

编辑:在评论中,## A tibble: 5 x 5 # var min max mean first # <chr> <dbl> <dbl> <dbl> <dbl> #1 X1Y 0.202 0.908 0.464 0.266 #2 X2Y 0.0618 0.945 0.639 0.898 #3 X3Y 0.177 0.770 0.445 0.206 #4 X4Y 0.380 0.992 0.673 0.498 #5 X5Y 0.126 0.935 0.438 0.935 不会更改我的顺序:

var