vendredi 25 septembre 2020

Print elements of vector in single line

I have a dataframe with 12 rows and two columns one with dates and the other with temperatures. I want to extract the values that meet the criteria in my if-statement (temperature is smaller than or equal to -20). This is my code:

for (row in 1:12) {
  T <- winter[row, 2]
  
  if(T <= -20) {
    x <-c(winter[row,2])
    print(x)
  }
}

Print x gives me this:

[1] -27
[1] -21
[1] -24
[1] -34
[1] -20

But I want something like this:

[1] -27, -21, -24, -34, -20

Dput:

dput(winter)
structure(list(date = 1:12, temp = c(-27L, -21L, -8L, -12L, 
-10L, -24L, 8L, -7L, -34L, -19L, -2L, -20L)), class = "data.frame", row.names = c(NA, 
-12L))

Aucun commentaire:

Enregistrer un commentaire