通过向量定义答案的函数

发布于 2024-12-11 01:52:03 字数 575 浏览 0 评论 0原文

希望学习函数编写。我的数据如下(例如):

Genus Species  Wing  Tail
 A       X     10.5  20.3
 A       Y     10.7  20.7
 B       XX    15.2  22.5
 B       XY    15.5  24

我使用等式计算给定特征的方差:

 sqrt(max(Wing) - min (Wing))

我对所有特征求和。

因此,我可以编写以下函数,对总数据集求和方差:

variance<- function(data){
t   <- sqrt(max(Tail)-min(Tail))
w   <- sqrt(max(Wing)-min(Wing))
x <- sum(t,w)
x
}

但我无法弄清楚如何生成响应以提供输出,其中该结果取决于属。所以我希望生成如下输出:

 Genus A    Genus B
  2.345      3.456

Looking to learn function writing. I have data laid out in the following (e.g.):

Genus Species  Wing  Tail
 A       X     10.5  20.3
 A       Y     10.7  20.7
 B       XX    15.2  22.5
 B       XY    15.5  24

I calculate variance for a given trait using the equation:

 sqrt(max(Wing) - min (Wing))

which I sum for all traits.

So I can write the following function so sum variance for the total data set:

variance<- function(data){
t   <- sqrt(max(Tail)-min(Tail))
w   <- sqrt(max(Wing)-min(Wing))
x <- sum(t,w)
x
}

But I can'twork out how to generate a response to give me an output where this result is dependant on the Genus. So i'm looking to generate an output like:

 Genus A    Genus B
  2.345      3.456

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2024-12-18 01:52:03

我将为您的函数起一个新名称,因为将其称为“方差”是错误的。我希望你能忽略这一点。我们可以处理数据帧对象

dput(dfrm)
structure(list(Genus = structure(c(1L, 1L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), Species = structure(c(1L, 4L, 2L, 3L
), .Label = c("X", "XX", "XY", "Y"), class = "factor"), Wing = c(10.5, 
10.7, 15.2, 15.5), Tail = c(20.3, 20.7, 22.5, 24)), .Names = c("Genus", 
"Species", "Wing", "Tail"), class = "data.frame", row.names = c(NA, 
-4L))

dev2<- function(df){
    t   <- sqrt(max(df[["Tail"]])-min(df[["Tail"]]))
    w   <- sqrt(max(df[["Wing"]])-min(df[["Wing"]]))
    x <- sum(t,w)
   x
   }

现在使用它来处理完整的数据帧,使用 split-lapply 策略,该策略将由 Genus 值确定的原始数据帧的部分传递给 dev2 函数

lapply( split(dfrm, list(dfrm$Genus)), FUN = dev2)
$A
[1] 1.079669

$B
[1] 1.772467

I am going to give a new name to your function because it's just wrong to call it "variance". I hope you can overlook that. We can work on a dataframe object

dput(dfrm)
structure(list(Genus = structure(c(1L, 1L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), Species = structure(c(1L, 4L, 2L, 3L
), .Label = c("X", "XX", "XY", "Y"), class = "factor"), Wing = c(10.5, 
10.7, 15.2, 15.5), Tail = c(20.3, 20.7, 22.5, 24)), .Names = c("Genus", 
"Species", "Wing", "Tail"), class = "data.frame", row.names = c(NA, 
-4L))

dev2<- function(df){
    t   <- sqrt(max(df[["Tail"]])-min(df[["Tail"]]))
    w   <- sqrt(max(df[["Wing"]])-min(df[["Wing"]]))
    x <- sum(t,w)
   x
   }

Now use it to work on the full dataframe, using the split-lapply strategy, which passes sections of the original dataframe determined by the Genus values to the dev2 function

lapply( split(dfrm, list(dfrm$Genus)), FUN = dev2)
$A
[1] 1.079669

$B
[1] 1.772467
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文