相关疑难解决方法(0)

一次重塑多个值

我有一个很长的数据集我想扩大,我很好奇是否有一种方法可以使用R中的reshape2或tidyr包一步完成所有这些操作.

数据框df如下所示:

id  type    transactions    amount
20  income       20          100
20  expense      25          95
30  income       50          300
30  expense      45          250
Run Code Online (Sandbox Code Playgroud)

我想谈谈这个问题:

id  income_transactions expense_transactions    income_amount   expense_amount
20       20                           25                 100             95
30       50                           45                 300             250
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过reshape2来获得部分路径,例如:

dcast(df, id ~  type, value.var="transactions")
Run Code Online (Sandbox Code Playgroud)

但有没有办法一次性重塑整个df,同时解决"交易"和"金额"变量?理想情况下,新的更合适的列名称?

r reshape dataframe reshape2 tidyr

17
推荐指数
2
解决办法
5912
查看次数

包装底座R重塑,易于使用

这是一个普遍公认的事实,R的基本重塑命令是快速而强大的,但具有悲惨的语法.因此,我已经编写了一个快速包装器,我将把它放到taRifx包的下一个版本中.然而,在我这样做之前,我想要求改进.

这是我的版本,来自@RichieCotton的更新:

# reshapeasy: Version of reshape with way, way better syntax
 # Written with the help of the StackOverflow R community
 # x is a data.frame to be reshaped
 # direction is "wide" or "long"
 # vars are the names of the (stubs of) the variables to be reshaped (if omitted, defaults to everything not in id or vary)
 # id are the names of the variables that identify unique observations
 # vary is the variable that …
Run Code Online (Sandbox Code Playgroud)

r reshape

14
推荐指数
1
解决办法
1272
查看次数

在R中重塑数据.是否有可能有两个"价值变量"

我正在努力使用reshape包寻找一种方法"强制转换"数据帧,但在"value.var"中有两个(或更多)值.

这是我想要实现的一个例子.

df <- data.frame( StudentID = c("x1", "x10", "x2", 
                            "x3", "x4", "x5", "x6", "x7", "x8", "x9"),
              StudentGender = c('F', 'M', 'F', 'M', 'F', 'M', 'F', 'M', 'M', 'M'),
              ExamenYear    = c('2007','2007','2007','2008','2008','2008','2008','2009','2009','2009'),
              Exam          = c('algebra', 'stats', 'bio', 'algebra', 'algebra', 'stats', 'stats', 'algebra', 'bio', 'bio'),
              participated  = c('no','yes','yes','yes','no','yes','yes','yes','yes','yes'),  
              passed      = c('no','yes','yes','yes','no','yes','yes','yes','no','yes'),
              stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)

从df我可以创建以下数据帧:

tx <- ddply(df, c('ExamenYear','StudentGender'), summarize,
        participated = sum(participated      == "yes"),
        passed   = sum(passed      == "yes"))
Run Code Online (Sandbox Code Playgroud)

在重塑逻辑中,我有两个"值变量"参与并通过

我正在寻找在一个数据帧中组合以下信息的方法:

 dcast(tx, formula = ExamenYear ~ StudentGender, value.var …
Run Code Online (Sandbox Code Playgroud)

r plyr reshape

6
推荐指数
1
解决办法
2318
查看次数

标签 统计

r ×3

reshape ×3

dataframe ×1

plyr ×1

reshape2 ×1

tidyr ×1