This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
convert_temperature <- function(value, from, to) {
if (from == to) {
return(value)
}
if (from == "C" && to == "F") {
return(value * 9/5 + 32)
} else if (from == "C" && to == "K") {
return(value + 273.15)
} else if (from == "F" && to == "C") {
return((value - 32) * 5/9)
} else if (from == "F" && to == "K") {
return((value - 32) * 5/9 + 273.15)
} else if (from == "K" && to == "C") {
return(value - 273.15)
} else if (from == "K" && to == "F") {
return((value - 273.15) * 9/5 + 32)
} else {
stop("Invalid input. Use 'C', 'F', or 'K' for temperature scales.")
}
}
# Example usage:
convert_temperature(100, "C", "F") # Output: 212
## [1] 212
convert_temperature(32, "F", "C") # Output: 0
## [1] 0
convert_temperature(300, "K", "C") # Output: 26.85
## [1] 26.85
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.