簡體   English   中英

(R)我正在嘗試使用if()語句引用數據框中的一列以計算其他多個列

[英](R) I'm trying to reference a column in a dataframe with an if() statement to compute multiple other columns

這是我創建的簡化數據集,用於說明我的問題。

數據

大家好,我正在嘗試引用數據框Database的列。 我的目標是能夠引用“權Weight列,並從中填充“ Risk和“ Overweight列。 這是我正在嘗試的內容(以及其他失敗的代碼):

ifelse(Database[,"Weight"] >190, Database$Risk="HIGH", Database$Risk="LOW")

Error: unexpected '=' in "ifelse(Database[,"Weight"] >190, Database$Risk="

我也嘗試過使用if()命令來編寫代碼組。

if(Database$Weight > 190) {Database$Risk="HIGH"; Database$Overweight="YES"}

Error in if (Database$Weight > 190) { : 
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In Ops.factor(Database$Weight, 190) : ‘>’ not meaningful for factors
2: In if (Database$Weight > 190) { :
the condition has length > 1 and only the first element will be used

...顯然我做得不好。

此代碼的理想輸出類似於:

所需的數據幀。

我們能避免ifelse與地方的分配使用data.table

library(data.table)
setDT(Database)[, Risk := "LOW"][Weight > 190, Risk := "HIGH"]

這是dplyr解決方案。 我假設Overweight也基於Weight分類。 您可以根據需要更改條件。

library(dplyr)
df %>%
  mutate(Risk=ifelse(Weight>190,"HIGH","LOW"), 
         Overweight=ifelse(Weight>190,"YES","NO"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM