简体   繁体   English

R:使用每个类别的不同方程按类别计算新变量

[英]R: Caculating new variable by category with a different equation for each category

I have been searching an answer to this for a while but nothing seems to fit me.我一直在寻找这个问题的答案一段时间,但似乎没有什么适合我。

I have a dataframe called "data" that looks like this:我有一个名为“数据”的 dataframe,如下所示:

Species物种 Length长度 Weight重量
A一个 15 15 0 0
A一个 8 8 0 0
B 20 20 0 0
C C 4 4 0 0
B 11 11 0 0
B 16 16 0 0
C C 13 13 0 0

What I would like to do is to calculate the weight of each individual of a species based on their length ant fill the Weigth column with the results.我想做的是根据物种的长度计算每个物种的重量 ant 用结果填充重量列。 For that purpose, I have already obtained an equation for each species, since each species has a different length-weight relation.为此,我已经为每个物种获得了一个方程,因为每个物种都有不同的长度-重量关系。 Lets say those length-weight equations look as follows:假设这些长度-重量方程如下所示:

  • "A" species: weight=length*2 “A”种:重量=长度*2
  • "B" species: weight=length^3 “B”种:重量=长度^3
  • "C" species: weight=length/5 “C”种:重量=长度/5

So, as a result, I would like to have have a dataframe that looks like this:因此,因此,我想要一个看起来像这样的 dataframe:

Species物种 Length长度 Weight重量
A一个 15 15 30 30
A一个 8 8 16 16
B 20 20 8000 8000
C C 4 4 0.8 0.8
B 11 11 1331 1331
B 16 16 4096 4096
C C 13 13 3.6 3.6

I have tried to do it this way:我试图这样做:

Species <- c("A", "A", "B", "C", "B","B","C")
Length <- c(15,8,20,4,11,16,13)
Weight <- c(0,0,0,0,0,0,0)
data <- data.frame(Species, Length, Weight)

for(i in 1:length(data$Length)){if(data$Species[i]=="A"){
data$Weight[i]<-data$Length[i]*2
}else if(data$Species[i]=="B"){
data$Weight[i]<-data$Length[i]^3
}else if(data$Species=="C"){
data$Weight[i]<-data$Length[i]/5
} else {data$Weight[i]<-"NA"}
}

The code runs without error, but the data$Weigth column is not filled with the results.代码运行没有错误,但 data$Weigth 列未填充结果。 However, when I tried the code without using for() and replacing [i] with a row number (for example [4]), it did the correct thing and fills the Weight column with the result for that row.但是,当我尝试代码而不使用 for() 并将 [i] 替换为行号(例如 [4])时,它做了正确的事情并用该行的结果填充了 Weight 列。

In reality I have 40 species and over 5000 observations, so running the code for each row individually is not an option.实际上,我有 40 个物种和超过 5000 个观察值,因此不能单独运行每一行的代码。

Any suggestions?有什么建议么? Maybe there is an easier way but I don´t see it?也许有更简单的方法,但我没有看到? Any help will be much appreciated.任何帮助都感激不尽。

Thanks谢谢

In base R you could do:在基础 R 中,您可以执行以下操作:

transform(df, 
  Weight = Vectorize(\(x, type) switch(type, A = x*2, B= x^3, C =x/5))(Length, Species))

  Species Length Weight
1       A     15   30.0
2       A      8   16.0
3       B     20 8000.0
4       C      4    0.8
5       B     11 1331.0
6       B     16 4096.0
7       C     13    2.6

library(tidyverse)
df %>%
  mutate(Weight = case_when(Species == 'A' ~Length * 2,
                            Species == 'B' ~ Length ^ 3,
                            Species == 'C' ~ Length / 5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM