Emmanuel Alagbe
Emmanuel Alagbe

Reputation: 305

R programming: I am trying to convert temperature from C to F

I am student working with R programming. I don't have a lot of experience with the software but I am willing to learn.

I am trying to convert temperature values from C to F and assigning the new values to soil_temp_f on a new column. I also intend achieving this with one line of code.

# seed germination
seeds <- data.frame(                            
  soil_temp_c = c(17.4, 15.5, 16.5, 15.4, 16.4, 16.3,       
                  16.6, 17.2, 17.5, 15.8, 18.3, 21.0),         
  n_seeds = c(27, 10, 20, 11, 21, 16, 16, 25, 24, 11, 27, 30)   
)

library(ggplot2)                                    
qplot(soil_temp_c, n_seeds, data = seeds)
select(seeds, (soil_temp_c<-soil_temp_c*(9/5)+32)) soil_temp_f)

The issue I am facing is on the last line as I keep getting an error that says "Error: unexpected symbol in select(seeds, (soil_temp_c<-soil_temp_c*(9/5)+32)) soil_temp_f)"

Upvotes: 1

Views: 2485

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388862

select is not the right function to create a new column. Try transform in base R

transform(seeds, soil_temp_f = soil_temp_c*(9/5)+32)

#   soil_temp_c n_seeds soil_temp_f
#1         17.4      27       63.32
#2         15.5      10       59.90
#3         16.5      20       61.70
#4         15.4      11       59.72
#5         16.4      21       61.52
#6         16.3      16       61.34
#7         16.6      16       61.88
#8         17.2      25       62.96
#9         17.5      24       63.50
#10        15.8      11       60.44
#11        18.3      27       64.94
#12        21.0      30       69.80

Or if you want to use dplyr, use mutate

library(dplyr)
seeds %>% mutate(soil_temp_f = soil_temp_c*(9/5)+32)

Upvotes: 2

Related Questions