if then do multiple statements from SAS to R -


i new r. here sas script want replicate in r:

if a=4 or b=4 do;   if a=1 or b=1 news="ab";   else if a=2 or b=2 news="bc";   else news="cd";   end; else do;   if a=1 or b=1 news="df";   else news="gh"; end; 

where: a, b variables (columns's name) of table/data, , news new variable want create.

i cannot find way of ifelse or other methods in r. can me? thank you!

here first try on first part of above script:

news <- ifelse(a==4 | b==4,            ifelse(a==1 | b==1, "ab",               ifelse(a==2 | b==2, "bc", "cd"))) 

but did not work. error shows "error in ifelse(a == 4 | b == 4, ifelse(a == 1 | b == : argument "no" missing, no default"

do know why? also, not sure how link second part, starting "else do" r. feel sas script easier understand , write in case r.

ifelse appropriate vectors (i.e., must test vector , output must vector of same length input), if() {...} else {...} single conditions. without sample input (and not knowing sas), i'm not sure you're after. if a , b single values, r schifini's answer good.

if a , b vectors, right use ifelse, , code works fine me. can extend include last else do statement:

test = expand.grid(a = c(1, 2, 4, 7), b = c(1, 2, 4)) = test$a b = test$b news <- ifelse(a == 4 | b == 4,                ifelse(a == 1 | b == 1, "ab",                       ifelse(a == 2 | b == 2, "bc", "cd")),                ifelse(a == 1 | b == 1, "df", "gh"))  cbind(test, news) #    b news # 1  1 1   df # 2  2 1   df # 3  4 1   ab # 4  7 1   df # 5  1 2   df # 6  2 2   gh # 7  4 2   bc # 8  7 2   gh # 9  1 4   ab # 10 2 4   bc # 11 4 4   cd # 12 7 4   cd 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -