按R中的列和组返回多个值 [英] Return multiple values from a function by column and group in R

查看:118
本文介绍了按R中的列和组返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为数据框中的每个因素找到大于20的数据比例,然后使用这些比例来计算2个其他值:
$ b $数据帧(num1 = as.numeric(c(10,30,4,60,20,1,34,87,66)),num2 = as.numeric(c(23,36,42,18,3,44,32,65,78)),num3 = as.numeric(c(0,0,0,20,80,10,50,43) ,第一组,第一组,第一组,第二组,第二组,第二组,第三组,第三组,第三组))

我想得到3个值(来自函数) num1,num2和num3,以及每个组如下所示:

$ $ $ $ code res = data.frame(cbind(col = c(rep(num1,3),rep(num2,3),rep(num3,3)),group = rep(c(First group,Second group ),3),p = c(0.3333333,0.333333,1.0000000,1.0000000,0.3333333,1.0000000,0.0000000,0.3333333,1.0000000),s1 = c(-0.1250000,-0.1250000,-0.2500000,-0.2500000,-0.1250000,-0 (0.1000000,0.1000000,0.5000000,0.5000000,0.1000000,0.5000000,0.0000000,0.1000000,0.5000000)))

我可以像这样返回每列的数据:

 <$ c ($ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ s $ (p,s1,s2))
(p / 2-p)/(p + 1)
s2 =(p / 2-p)/ }

ddply(dat,。(group),summarize,prop(num1))

但我不明白如何将它们绑定到一个数据框并应用到每一列。我尝试过不同的方式(例如这个,但它不适用于我,因为我一直只有一列,我试图通过使用ggplot2按组来绘制这些值
你能帮我吗?

解决方案

  prop < -  function(s){
n = length (s [s> 20])
p = x / n
s1 =(p / 2-p)/(p + 1)
s2 = (p / 2-p)/(p-2)
data.frame(p,s1,s2)
}

library(reshape2)
dat< ; - 熔化(dat,id =group)
库(plyr)
ddply(dat,。(variable,group),function(df)prop(df $ value))

#变量组p s1 s2
#1 num1第一组0.3333333 -0.125 0.1
#2 num1第二组0.3333333 -0.125 0.1
#3 num1第三组1.0000000 -0.250 0.5
#4 num2第一组1.0000000 -0.250 0.5
#5 num2第二组0.3333333 -0.125 0.1
#6 num2第三组1.0000000 -0.250 0.5
#7 num3第一组0.0000000 0.000 0.0
#8 num3第二组0.3333333 -0.125 0.1
#9 num3第三组1.0000000 -0.250 0.5


I am trying to find the proportion of data that is greater than 20 for each of the factors I have in the data frame, then use those proportions to compute 2 other values:

dat <- data.frame(num1=as.numeric(c(10,30,4,60,20,1,34,87,66)), num2=as.numeric(c(23,36,42,18,3,44,32,65,78)), num3=as.numeric(c(0,0,0,20,80,10,50,43,70)), group=c("First group", "First group","First group", "Second group","Second group","Second group", "Third group","Third group","Third group"))

I would like to get 3 values (from a function) computed for each of the columns num1, num2 and num3, and each of the groups like this:

res = data.frame(cbind(col=c(rep("num1",3), rep("num2",3), rep("num3",3)), group=rep(c("First group", "Second group","Third group"),3) , p= c(0.3333333, 0.3333333, 1.0000000,1.0000000, 0.3333333,1.0000000,0.0000000,0.3333333,1.0000000), s1= c(-0.1250000, -0.1250000, -0.2500000,-0.2500000,-0.1250000,-0.2500000,0.0000000,-0.1250000,-0.2500000), s2= c(0.1000000, 0.1000000, 0.5000000,0.5000000, 0.1000000, 0.5000000, 0.0000000,0.1000000,0.5000000)))

I can get as far as returning data for each column like this:

prop <- function(s) {
 n= length(s)
 x=length(s[s>20])
 p=x/n
 s1=(p/2-p)/(p+1)
 s2=(p/2-p)/(p-2)
 return(c(p,s1,s2))
 }

ddply(dat, .(group), summarise, prop(num1))

but then I don't understand how to bind them into a dataframe and apply to each columns. I have tried different ways (for example this but it is not working for me as I keep getting only one column. I am trying to do this by the way to then plot these values by group using ggplot2. Can you please help me?

解决方案

prop <- function(s) {
  n= length(s)
  x=length(s[s>20])
  p=x/n
  s1=(p/2-p)/(p+1)
  s2=(p/2-p)/(p-2)
  data.frame(p,s1,s2)
}

library(reshape2)
dat <- melt(dat, id="group")
library(plyr)
ddply(dat, .(variable, group), function(df) prop(df$value))

#  variable        group         p     s1  s2
#1     num1  First group 0.3333333 -0.125 0.1
#2     num1 Second group 0.3333333 -0.125 0.1
#3     num1  Third group 1.0000000 -0.250 0.5
#4     num2  First group 1.0000000 -0.250 0.5
#5     num2 Second group 0.3333333 -0.125 0.1
#6     num2  Third group 1.0000000 -0.250 0.5
#7     num3  First group 0.0000000  0.000 0.0
#8     num3 Second group 0.3333333 -0.125 0.1
#9     num3  Third group 1.0000000 -0.250 0.5

这篇关于按R中的列和组返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆