RPubs will retire in June 2027. Your existing documents will stay accessible through December 31, 2031
and Connect Cloud is the recommended home for new publishing. Read the blog post
gravatar

POPcorn789

POPcorn789

Recently Published

Document
1160730303
BMI
Plot
Plot
Plot
Plot
Plot
1+1 2*3 9/2 x<-2 y<-3 z<-x+y w<-c(4,6,8) w w/x #建立身高、體重資料物件 height <-1.77 weight <-72 #建立BMI公式,並命名為bmi bmi<-weight/height^2 bmi <- round(bmi,2)#取小數點到第二位 bmi_j<-bmi>18.5 & bmi<24#判斷bmi是否在正常範圍 bmi_j ##向量(vector)運算 weight_g <- c(70, 62, 85, 60, 75) height_g <- c(1.7, 1.68, 1.82, 1.62, 1.75) bmi_g<-weight_g/height_g^2 bmi_g bmi_g <- round(bmi_g,2) bmi_g_j<-bmi_g>18.5 & bmi_g<24 bmi_g_j bmi_g_j <- 請輸入(bmi_g_j)#用平均數計算bmi合格率(邏輯值也可以運算。FALSE=0, TRUE=1) bmi_g_j #為向量命名 names(weight_g)<-c("A","B","C","D","E") weight_g #選擇vector內元素 weight_g[3] weight_g[1:3] weight_g[weight_g>70] #數學運算 length(weight_g)#計算向量內的元數個數 mean(weight_g) max(weight_g) which.max(weight_g) sum(weight_g) min(weight_g) sort(weight_g)#排序(預設是由小到大) sort(weight_g, decreasing = TRUE)#由大到小 #將身高換算為英吋(1 inch = 2.54 cm),取小數點後一位 height_g round(height_g*100/2.54,1) ##畫長條圖 barplot(weight_g, col = c("skyblue", "magenta", "oldlace", "azure", "tomato4")) colors()#查詢R的內建顏色 barplot(weight_g, col = c(6:10))#用內建預設的第6種到第10種顏色 BMI <- function(height, weight){ bmi <- weight/(height^2) return(bmi) } BMI_J <- function(height, weight){ bmi <- weight/(height^2) bmi_j <- bmi>18.5 & bmi<24 return(bmi_j) }
Plot