【问题标题】:Is there a way to streamline this code using tidyverse?有没有办法使用 tidyverse 简化此代码?
【发布时间】:2022-11-16 08:52:07
【问题描述】:

我目前正在尝试找到一种更简化的方法来运行所提供的代码。基本上它是通过一个巨大的数据库并提取出现在 20% 或更多地层上的植物物种。然后需要计算零值以正确显示整个地层的发生情况。

我的代码在下面列出并且工作正常但是你必须替换物种代码,生长习性,零出现次数等。所以不是最精简的。

抱歉,如果某些格式不正确,长时间观看者第一次询问。感谢您的帮助!

##E_Low STRATA
##list of dominant species in order of species with highest percent occurence per plot, note this step is only to determine the species
##present on 20% of the plots in the stratum.  columns created will be used/Modified later in the code


E_LowD<- PSPS1 %>%
  select(PrimaryKey,AH_SpeciesCover,Species,Elevation_CL,GrowthHabit) %>%
  group_by(Elevation_CL) %>%
  filter(Elevation_CL=="E_Low") %>%
  group_by(Species) %>%
  dplyr::mutate(N_category=n()) %>% 
  count(c("PrimaryKey","AH_SpeciesCover","Species","Elevation_CL", "GrowthHabit","N_category")) %>%
  ungroup() %>%
  mutate(dom_ss=N_category/length(unique(PrimaryKey))) %>%
  filter(dom_ss >= .2) %>%
  group_by(Species) %>%
  mutate(SS_avg=sum(AH_SpeciesCover)/length(unique(PrimaryKey)))


E_Low<- E_LowD %>%
  mutate(zero=(length(unique(E_LowD$PrimaryKey))-N_category)) %>%
  arrange(desc(dom_ss))
PSPS3<-E_Low[!duplicated(E_Low$Species),]
PSPS4<- PSPS3 %>%
  select(Species)

formattable(PSPS4)

##the zero output will show how many plots for each species need to have a 0 value added in order to properly represent the data


E_Low1<-head(PSPS3,4)
E_Low2<-E_Low1 %>%
  select(Species,zero)
formattable(E_Low2)


##adding plots with 0 value, replace domss and species name and zero # based on results of last step

domss<-c("BRTE","ARTRW8", "ALDE","SAVE4")
E_LowA<-E_Low %>%
  filter(Species %in% domss)
E_Low6<-E_LowA %>%
  select(Species,AH_SpeciesCover,PrimaryKey,GrowthHabit)
E_Low7<-as.data.frame(E_Low6)

#(species name, zero #)

A<-rep("BRTE",4)
B<-rep("ARTRW8",10)
C<-rep("ALDE",16)
D<-rep("SAVE4",16)

#(GrowthHabit, zero #)

A2<-rep("NonWoody",4)
B2<-rep("Woody",10)
C2<-rep("NonWoody",16)
D2<-rep("Woody",16 )
Species_list<-c(A,B,C,D)


4+10+16+16



在此输入

    #enter total of above replicate(*TotalNum*,0)     PrimaryKey_list<-1:*TotalNum
AH_SpeciesCover_list<-replicate(46,0)
PrimaryKey_list<-1:46
GrowthHabit_List<-c(A2,B2,C2,D2)
zeros<-data.frame(Species=Species_list,AH_SpeciesCover=AH_SpeciesCover_list,PrimaryKey=PrimaryKey_list,GrowthHabit=GrowthHabit_List)
E_Low8<-rbind(as.data.frame(E_Low7),zeros)

##dom E_Low boxplot

myColors<-c("black","darkgreen","sienna4")
names(myColors)<-levels(E_Low8$GrowthHabit)
colScale<-scale_colour_manual(name = "GrowthHabit",values = myColors)


jpeg(filename="FIGURES_FINAL/E_Low-dom_Cover.jpeg", width=12, height =8, units = "in", res=300) 


E_Low8 %>% 
  ggplot(aes(x=Species, y = AH_SpeciesCover,fill = GrowthHabit))+
  geom_boxplot()+
  geom_jitter(alpha = 0.25)+
  ggtitle(paste0(E_Lowpop))+
  theme_bw()+
  labs(x="Plant Species Code",y="Average Percent Cover",colour="Growth Form")+
  facet_grid(.~GrowthHabit, scale = "free", drop= TRUE)+
  theme(axis.text.x=element_text(colour="gray20"))+
  colScale + 
  scale_fill_discrete(name = "Growth Habitat")

dev.off()

【问题讨论】:

  • 你能分享一个小的说明性数据样本吗?最好使用dput(),以便可以复制/粘贴?例如,dput(PSPS1[1:20, ]) 表示前 20 行
  • 如果此代码有效,您可能需要考虑将其发布到code review,它专门用于优化工作代码

标签: r tidyverse


【解决方案1】:

两个想法。首先,您可以在不构建中间变量的情况下创建列表和 data.frames:

例如,这个长位

A<-rep("BRTE",4)
B<-rep("ARTRW8",10)
C<-rep("ALDE",16)
D<-rep("SAVE4",16)
Species_list<-c(A,B,C,D)

可能是这样的:

Species_list<-c(rep("BRTE",4),
                rep("ARTRW8",10),
                rep("ALDE",16),
                rep("SAVE4",16))

data.frames做同样的事情

其次,您可以通过管道将多个命令组合在一起,而不是将它们传递到中间对象中。例如这个:

E_LowA<-E_Low %>%
  filter(Species %in% domss)
E_Low6<-E_LowA %>%
  select(Species,AH_SpeciesCover,PrimaryKey,GrowthHabit)

变成:

E_LowA<-E_Low %>%
  filter(Species %in% domss) %>%
  select(Species,AH_SpeciesCover,PrimaryKey,GrowthHabit)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    相关资源
    最近更新 更多