简体   繁体   English

编写更简洁的代码版本以提高可读性

[英]Writing a cleaner version of code to improve readability

I am fairly new to R and I am hoping to know if there is a cleaner way of writing the code below.我对 R 相当陌生,我希望知道是否有更简洁的方法来编写下面的代码。 Basically, I am dropping a few duplicates from df1 and appending them to df2.基本上,我从 df1 中删除了一些重复项并将它们附加到 df2。 I am then joining the values from count_df to df2 and renaming certain columns in df2.然后我将 count_df 中的值连接到 df2 并重命名 df2 中的某些列。

I would really like to do all of this under a single code block as opposed to the way I have done it below.我真的很想在一个代码块下完成所有这些,而不是我在下面完成的方式。 However, I keep running very different errors every time I try to put this all under a single code block.但是,每次我尝试将这一切都放在一个代码块下时,我都会遇到非常不同的错误。 This is just a single instance, but I have had to do this multiple times already in my code.这只是一个实例,但我已经在我的代码中多次这样做了。 I would greatly appreciate any input on how to improve the code below.对于如何改进下面的代码,我将不胜感激。 Thank you!谢谢!

df2 <- df1 %>%
  distinct(ID, Temp, .keep_all = TRUE) %>%
  summarise(n())

df2 <- df2 %>%
  inner_join(count_df, by = c("ID" = "ID"))

df2 <- df2 %>%
  rename(unique_dps = `n()`,inital_dps = n)

We can have this in the same %>%我们可以在同一个%>%

library(dplyr)
count_df %>%
        add_count(ID, Temp) %>% 
        rename(unique_dps = n)
           

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

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