2017-07-15 125 views
0

我在Microsoft R中有一個XDF文件。這是我的問題的一個簡化版本。它有2列,其中一列是分類的,並且包含諸如「1」,「2」,「3」等的指標...一直到「10」。第二欄是數字。我想要做的是創建一個3列,這是第二列的變換,但是以第一列爲條件。即如XDF文件的條件轉換

if col1 == '1' then col3 = col2*0.50 else if col2 == '2' then col3 = col2*0.80 

等等。

我知道你可以直接在R中進行變換,但我不知道如何完成這樣的事情。

回答

0

這是行不通的嗎?

MyDataset <- RxXdfData("./path/dataset.xdf") #the dataset you are using 

# replace transFormedCol with the name of the col you want to use 
rxDataStep(inData = MyDataset, 
      outFile = MyDataset, 
      transforms = list(
      transFormedCol = ifelse(col1 == '1',col2*0.5, 
            ifelse(col2 == '2', col2*0.8))), #etc... 
      overwrite = TRUE 
      )