本文介绍了在 R: dcast in function 中,传递列名(再次!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个半长格式的 df,id 变量为 ab 以及 m1m2.数据类型由变量 v(值 var1 和 var2)指定.

Given a df in semi-long format with id variables a and b and measured data in columns m1and m2. The type of data is specified by the variable v (values var1 and var2).

set.seed(8)

df_l <- 
  data.frame(
    a = rep(sample(LETTERS,5),2),
    b = rep(sample(letters,5),2),
    v = c(rep("var1",5),rep("var2",5)),
    m1 = sample(1:10,10,F),
    m2 = sample(20:40,10,F)) 

看起来像:

   a b    v m1 m2
1  W r var1  3 40
2  N l var1  6 32
3  R a var1  9 28
4  F g var1  5 21
5  E u var1  4 38
6  W r var2  1 35
7  N l var2  8 33
8  R a var2 10 29
9  F g var2  7 30
10 E u var2  2 23

如果我想在 m1 中使用 id a 作为行和在 v1 中作为列的值来制作宽格式的值,我会这样做:

If I want to make a wide format of values in m1 using id a as rows and values in v1as columns I do:

> reshape2::dcast(df_l, a~v, value.var="m1")
  a var1 var2
1 E    4    2
2 F    5    7
3 N    6    8
4 R    9   10
5 W    3    1

如何编写一个函数来执行此操作,将 dcast(行、列和 value.var)的参数作为参数提供,例如:

How do I write a function that does this were arguments to dcast (row, column and value.var) are supplied as arguments, something like:

fun <- function(df,row,col,val){
  require(reshape2)
  res <-
    dcast(df, row~col, value.var=val)
  return(res)
}

我在这里检查了这里 尝试 match.calleval(substitute()) 的变体以获取"函数内部的参数,并尝试使用 lazyeval 包.没有成功.

I checked SO here and here to try variations of match.call and eval(substitute()) in order to "get" the arguments inside the function, and also tried with the lazyeval package. No succes.

我在这里做错了什么?如何让 dcast 识别变量名?

What am I doing wrong here ? How to get dcast to recognize variable names?

推荐答案

公式参数也接受字符输入.

Formula argument also accepts character input.

foo <- function(df, id, measure, val) {
    dcast(df, paste(paste(id, collapse = " + "), "~", 
                    paste(measure, collapse = " + ")), 
          value.var = val)
}

require(reshape2)
foo(df_l, "a", "v", "m1")

注意 data.tabledcast (当前开发) 也可以直接转换多个 value.var 列.所以,你也可以这样做:


Note that data.table's dcast (current development) can also cast multiple value.var columns directly. So, you can also do:

require(data.table) # v1.9.5
foo(setDT(df_l), "a", "v", c("m1", "m2"))
#    a m1_var1 m1_var2 m2_var1 m2_var2
# 1: F       1       6      28      21
# 2: H       9       2      38      29
# 3: M       5      10      24      35
# 4: O       8       3      23      26
# 5: T       4       7      31      39

这篇关于在 R: dcast in function 中,传递列名(再次!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 03:14