2016-06-29 6 views
0

SPSSからRに変換しようとしています。私はRを学ぶ良い方法として私の掃除構文を翻訳したいと思っていましたが、私はSPSSの単純なコマンドとは何か大きな問題を抱えています。多数の変数を多くの変数やステートメントで1つの変数に再コード化する

次のコードは、SPSSで動作します。

IF (n_race = 1 & (n_ethnicity = 7 | n_ethnicity = 8 | n_ethnicity = 99)) race_eth=1. 
EXECUTE. 
IF (n_ethnicity = 3 & (n_race = 1 | n_race = 99)) race_eth=2. 
EXECUTE. 
IF (n_race = 2) race_eth=3. 
EXECUTE. 
IF (n_race = 5 & n_ethnicity NE 9) race_eth=4. 
EXECUTE. 
IF ((n_ethnicity = 2 | n_ethnicity = 4 | n_ethnicity = 5) & (n_race = 1 | n_race = 99)) race_eth=4. 
EXECUTE. 
IF (n_ethnicity = 9 & (n_race = 1 | n_race = 5 | n_race = 99)) race_eth=5. 
EXECUTE. 
IF (n_race = 4 | (n_ethnicity = 6 & (n_race = 1 | n_race = 99))) race_eth=6. 
EXECUTE. 
IF (n_ethnicity = 1 & (n_race = 1 | n_race = 99)) race_eth=6. 
EXECUTE. 
IF (n_race=99 AND (n_ethnicity=7 OR n_ethnicity=99 OR n_ethnicity=8)) race_eth=99. 
EXECUTE. 

私は、このコードはRで同じことをするだろうと思ったが、セルの周波数は、(私はRでn_ethするn_ethnicityと改名することに注意してください)同じではありません。私は大いに助けていただければ幸いです!ありがとう!

OMSOct14_Mar16$race_eth[n_race == 1 & (n_eth == 7 | n_eth == 8 | n_eth == 99)] <- 1 
OMSOct14_Mar16$race_eth[n_eth == 3 & (n_race == 1 | n_race == 99)] <- 2 
OMSOct14_Mar16$race_eth[n_race == 2] <- 3 
OMSOct14_Mar16$race_eth[n_race == 5 & n_eth != 9] <- 4 
OMSOct14_Mar16$race_eth[(n_eth == 2 | n_eth==4 | n_eth==5) & (n_race == 1 | n_race == 99)] <- 4 
OMSOct14_Mar16$race_eth[n_eth == 9 & (n_race == 1 | n_race == 5 | n_race==99)] <- 5 
OMSOct14_Mar16$race_eth[n_race == 4 | (n_eth == 6 & (n_race==1 | n_race == 99))] <- 6 
OMSOct14_Mar16$race_eth[n_eth == 1 & (n_race == 1 | n_race == 99)] <- 6 
OMSOct14_Mar16$race_eth[n_race == 99 & (n_eth == 7 | n_eth == 8 | n_eth==99)] <- 99 
+0

。 'OMSOct14_Mar16 $ race_eth [OMSOct14_Mar16 $ n_race == 2] < - 3' – thelatemail

+0

のように、変数が明示的に指定するデータセットを参照し続ける必要があります。脇に、' n_ethnicity = 2 | n_ethnicity = 4 | n_ethnicity = 5'は%c(2,4,5) 'のn_ethnicity%に変換することができます。これはずっと簡単です。 – thelatemail

+0

何らかの理由で99という値が7になったことに気付きませんでした。ラベルを貼るときにRが変数を注文すると思いますか?それは働いて、簡素化のためにあなたに感謝!私は自分でそれを非常に難しくしていました。 – KCP

答えて

1

あなたはwithifelseを使用して、このような何かを行うことができます:あなたはおそらくn_eth`と `OMSOct14_Mar16 $ n_eth`は十分に異なる場合があります`異なる変数を参照している

set.seed(99) 
n_race<-c(1,99,2,5,4) 
n_eth<-c(7,8,99,3,9,2,4,5,6,1) 
OMSOct14_Mar16<-data.frame(n_race = sample(n_race, 20, replace = T), 
          n_eth = sample(n_eth, 20, replace = T)) 

OMSOct14_Mar16$race_eth<- 
    with(OMSOct14_Mar16,ifelse(n_eth == 1 & n_eth %in% c(7,8,99), 1, 
          ifelse(n_eth == 3 & n_race %in% c(1,99), 2, 
          ifelse(n_race == 2, 3, 
          ifelse(n_race==5 & n_eth !=9, 4, 
          ifelse(n_eth %in% c(2,4,5) & n_race %in% c(1,99), 4, 
          ifelse(n_eth == 9 & n_race %in% c(1,5,99), 5, 
          ifelse(n_race ==4 | n_eth == 6 & (n_race %in% c(1,99)), 6, 
          ifelse(n_eth == 1 & n_race %in% c(1,99), 6, 
          ifelse(n_race == 99 & (n_eth %in% c(7,8)), 99, NA)))))))))) 

head(OMSOct14_Mar16,5) 
n_race n_eth race_eth 
2 99  3 
1  7  NA 
5  6  4 
4  2  6 
2  5  3 
+0

これも私が試していたものの大きな単純化です。私がこのことを学ぶのを助けてくれてありがとう/もっと良くする! – KCP

関連する問題