我有一個班級作業,任務是在 R 中創建一個函式,該函式接受一個值向量并將它們在華氏、攝氏和開爾文之間轉換。我嘗試通過我的代碼運行的示例應該生成一個僅包含 3 個值的回傳向量,但它回傳一個包含 26 個值的向量。
下面是我的代碼:
convTemp <- function(x, from = "C", to = "F") {
newX <- vector("double", length(x))
if (from == to) {
warning("Your 'from' parameter is the same as your 'to' parameter!") # From and To are same temperature scale
}
if (from == "C" && to == "F") { # Celsius to Fahrenheit
for (i in x) {
newX[i] <- ((9/5)*x[i] 32)
}
}
if (from == "C" && to == "K") { # Celsius to Kelvin
for (i in x) {
newX[i] <- (x[i] 273.15)
}
}
if (from == "F" && to == "C") { # Fahrenheit to Celsius
for (i in x) {
newX[i] <- ((x[i]-32)*(5/9))
}
}
if (from == "K" && to == "C") { # Kelvin to Celsius
for (i in x) {
newX[i] <- (x[i]-273.15)
}
}
if (from == "F" && to == "K") { # Fahrenheit to Kelvin
for (i in x) {
newX[i] <- ((((x[i]-32)*5)/9) 273.15)
}
}
if (from == "K" && to == "F") { # Kelvin to Fahrenheit
for (i in x) {
newX[i] <- ((((x[i]-273.15)*9)/5) 32)
}
}
return(newX)
}
convTemp(c(35,40,45), from="F", to="K")
這是我收到的輸出:
[1] 0 0 0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
所以我不確定為什么該函式應該回傳一個包含 3 個 Kelvin 值的向量時回傳如此大的缺失值向量。
uj5u.com熱心網友回復:
由于 R 是矢量化的,因此您不需要 for 回圈。檢查下面的代碼以了解如何更好地撰寫函式:
convTemp <- function(x, from = "C", to = "F") {
stopifnot(c(from, to) %in% c('C', 'F', 'K'))# IN CASE YOU INPUT A DIFFERENT ARGUMENT OTHER THAN C,F, AND K
if (from == to) {
warning("Your 'from' parameter is the same as your 'to' parameter!") # From and To are same temperature scale
}
if (from == "C"){
if (to == "F") 9/5 * x 32 # Celsius to Fahrenheit
else (x 273.15) # Celsius to Kelvin
}
else if (from == "F") {
if(to == "C") (x -32) * 5/9 # Fahrenheit to Celsius
else (x - 32) * 5/9 273.15 # Fahrenheit to Kelvin
}
else {
if(to == 'C')(x - 273.15) # Kelvin to Celsius
else (x-273.15) * 9/5 32 # Kelvin to Fahrenheit
}
}
convTemp(c(35,40,45), from="F", to="K")
[1] 274.8167 277.5944 280.3722
uj5u.com熱心網友回復:
在代碼中(假設這是一個編碼練習),我們不需要回圈遍歷值 ( for(i in x)),而是回圈遍歷序列 ( for(i in seq_along(x)))
convTemp <- function(x, from = "C", to = "F") {
newX <- vector("double", length(x))
if (from == to) {
warning("Your 'from' parameter is the same as your 'to' parameter!") # From and To are same temperature scale
}
if (from == "C" && to == "F") { # Celsius to Fahrenheit
for (i in seq_along(x)) {
newX[i] <- ((9/5)*x[i] 32)
}
}
if (from == "C" && to == "K") { # Celsius to Kelvin
for (i in seq_along(x)) {
newX[i] <- (x[i] 273.15)
}
}
if (from == "F" && to == "C") { # Fahrenheit to Celsius
for (i in seq_along(x)) {
newX[i] <- ((x[i]-32)*(5/9))
}
}
if (from == "K" && to == "C") { # Kelvin to Celsius
for (i in seq_along(x)) {
newX[i] <- (x[i]-273.15)
}
}
if (from == "F" && to == "K") { # Fahrenheit to Kelvin
for (i in seq_along(x)) {
newX[i] <- ((((x[i]-32)*5)/9) 273.15)
}
}
if (from == "K" && to == "F") { # Kelvin to Fahrenheit
for (i in seq_along(x)) {
newX[i] <- ((((x[i]-273.15)*9)/5) 32)
}
}
return(newX)
}
-測驗
> convTemp(c(35,40,45), from="F", to="K")
[1] 274.8167 277.5944 280.3722
uj5u.com熱心網友回復:
不是你問的,而是因為 @onyambu 幫助消除了 for 回圈,我想我可能會幫助消除 if 陳述句。我不確定這是否更有效,但我發現有時很難遵循一堆 if 陳述句。
convTemp <- function(x, from = "C", to = "F") {
stopifnot(c(from, to) %in% c('C', 'F', 'K'))
from. <- c(0,-32,-273.15)[which(c('C', 'F', 'K') %in% from)]
to. <- c(0,32,273.15)[which( c('C', 'F', 'K') %in% to)]
mult. <- c(1, 5/9, 9/5 )[sum(which(c(from, to) %in% 'F')) 1]
(x from.)*mult. to.
}
convTemp(c(35,40,45), from="F", to="K")
#> [1] 274.8167 277.5944 280.3722
convTemp(c(35,40,45), from="C", to="F")
#> [1] 95 104 113
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521636.html
標籤:r功能向量
上一篇:平均每行的每個重復列?
