Programming R dynamically

Hello dear world,

I am a bloody beginner and struggeling to write a code that changes dynamically and was hoping someone could help me.

SO basically I have an exel table with Prices and different properties of which each property should be correlated to the price.

Question no. 1: How do I programm in R so that I don't have to keep changing the changing input veriable (property 1, property 2,...)?

I have got as far as:

cor.create <- function (my_data) { 
  cor (my_data$x, my_data$y, method = "spearman")}

so in the coding I would like to be abble to change the "y" to different variables without typing? (I have a large amount of data for my master thesis)

Question no. 2: Before that I would like to do scatterplots. How can I make R change the title of the pdf its creating fit to the input data?
I have the following code:

function (my_data)  {
  ggplot(data = my_data, mapping = aes (x, y)) + 
    geom_point() + 
    labs (title = 'my_data', x = 'Preis (in Euro pro 100g)', y = 'Qualitätsurteil der Stiftung Warentest') + 
    ggsave("my_data.pdf", plot = last_plot()) }

so in the coding "my_data.pdf" I would like R to change this according to what I put in as "my_data"?

Question no. 3: If I have the data stored in different Variables, is it possible to get R to use my built functions with each of these?

Sorry to bother you. Any tips also as in where I can find help for these questions will be very much appreciated!

Lia

I think this post contains the answer to at least a couple of your questions:
https://www.brodrigues.co/blog/2017-03-29-make-ggplot2-purrr/

2 Likes

Hi @Lila,

Regarding Q1, try to take a look at this for correlating mutliple variables:

# Load libraries
library('tidyverse')

# Create dummy data
data = tibble(d_1 = rep(NA, 100))
for( i in 1:10 ){
  data = data %>% mutate(!!str_c('d_', i) := rnorm(100))
}

# Correlate all variables
cor(data)

Hope it helps :slightly_smiling_face:
Leon

1 Like

Lia, welcome to R programming. It looks like you're new to programming, so welcome to the journey!

I'll try to answer some bits of what you asked.

Q1) this depends on how your data is structured. Does your data have each input variable as a different column in the data frame like df_wide below or is does it have a variable that contains the name of the variable, like df_long below?



library(tidyverse)
n <- 5
df_wide <- data.frame(
  d1 = rnorm(n),
  d2  = rnorm(n),
  d3 = rnorm(n),
  d4 = rnorm(n)
)

df_wide
#>            d1          d2         d3          d4
#> 1 -0.30013188 -1.15724871 -0.6842435 -1.69573506
#> 2  0.07135999  0.37691683 -0.5642811 -1.14831895
#> 3  0.07540957 -0.05911097  1.0084456  1.31179016
#> 4  1.04405814 -0.72960569  0.7876833 -0.89300680
#> 5 -0.27028056 -1.02603006 -1.4922447 -0.05271003

df_long <- gather(df_wide, key="variable", value="value")

df_long
#>    variable       value
#> 1        d1 -0.30013188
#> 2        d1  0.07135999
#> 3        d1  0.07540957
#> 4        d1  1.04405814
#> 5        d1 -0.27028056
#> 6        d2 -1.15724871
#> 7        d2  0.37691683
#> 8        d2 -0.05911097
#> 9        d2 -0.72960569
#> 10       d2 -1.02603006
#> 11       d3 -0.68424352
#> 12       d3 -0.56428113
#> 13       d3  1.00844559
#> 14       d3  0.78768332
#> 15       d3 -1.49224469
#> 16       d4 -1.69573506
#> 17       d4 -1.14831895
#> 18       d4  1.31179016
#> 19       d4 -0.89300680
#> 20       d4 -0.05271003

if your data looks like df_wide you can just call cor on the data frame:

cor(df_wide, method="spearman")
#>      d1   d2   d3  d4
#> d1  1.0  0.5 -0.4 0.1
#> d2  0.5  1.0 -0.6 0.4
#> d3 -0.4 -0.6  1.0 0.3
#> d4  0.1  0.4  0.3 1.0

The result is a matrix object.

Q2) set the name in a text variable then use paste(my_filename, ".pdf") to change the file name. But you may want to do many pairwise plots on one graph. One easy way to do this is with the GGally package:

library(GGally)

ggpairs(df_wide)

Q3) yes, R is a programming language so you can make it do almost anything that you can describe. You'd be really well served to spend a couple of days reading R for Data Science: https://r4ds.had.co.nz

R4DS will help you get oriented as to what is possible and how to structure your analysis.

Created on 2019-01-10 by the reprex package (v0.2.1)

3 Likes

Thank you! That was really helpful. My problem with most help pages and informations on R is that the codes are so short. Yours was perfect for me to understand! Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.