-1

I would like to run the same code but change the name of the data frame each time so I can run the same analysis for several different datasets I have. Is there a more efficient way to alter my code for a instance with a simple replace function or would I have to do it manually each time?

This is what my code looks like:

    ```{r}
jd_df_wthr_corr <- subset(jd_df_wthr_lag, year < 2016) 
```

##Correlation of temperature (avg)

```{r}
jd_df_wthr_corr %>%
  ggplot(aes(logn, mean_temp)) +
  geom_point() +
  geom_smooth(method ="lm", se=FALSE)

cor.test(jd_df_wthr_corr$logn, jd_df_wthr_corr$mean_temp)
```


### with a 1 week lag

```{r}
jd_df_wthr_corr %>%
  ggplot(aes(lag_1week, mean_temp)) +
  geom_point() +
  geom_smooth(method ="lm", se=FALSE)

cor.test(jd_df_wthr_corr$lag_1week, jd_df_wthr_corr$mean_temp, use ="complete")
```

This goes on for about 2500 lines. I want to run the same code but replace every instance of "jd" with "jz" or every instance of "jd_df_wthr_corr" with "jz_df_wthr_corr". I notice that when I double click on a specific text it highlights that same text everywhere it appears on the code, which makes me think there should be a way to replace it everywhere.

  • You have identified one of the main reasons that R allows people to define functions. – John Coleman Apr 15 at 23:50
1
results <- sapply(c(frame1, frame2), function(df) { return(run.analysis(df)) }, simplify=TRUE)

If it doesn't work, leave a comment, but I hope that helps.

  • I've added some sample code to my original question. How would I incorporate your suggested code to get the result I'm looking for? – Ktass Apr 16 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.