0

I used sqldf packges in R.

I tried:

project=sqldf('select distinct
              ID
              , sex  
              , case when age<30 then "1" --20대
                     when age<40 then "2" --30대
                     when age<50 then "3" --40대
                     when age<60 then "4" --50대
                     when age<70 then "5" --60대
                     when 70<=age then "6" --70대 
                     end as age
              , ho_incm
              , town_t
              , case when tins in (10, 20) then "1" --국민건강보험
                   when tins=30 then "2" --의료급여
                   end as tins
              , edu 
              , marri_1 in (1, 2) then marri_1 --1:기혼, 2:이혼
                   end as marri_1
              , case when EC1_1 in (1, 2) then EC1_1 --1:취업, 2:실업
                   end as EC1_1
              , case when BS3_1 in (1, 2) then "1" --현재흡연
                     when BS3_1=3 then "2" --과거흡연
                     when BS3_1=8 then "3" --비흡연
                     end as BS3_1
              , case when BD1_11 in (1, 2) then "2" --비음주
                     when BD1_11 in (3, 4, 5, 6) then "1" --음주
                     end as BD1_11
              , case when BP1 in (1, 2) then "1" --스트레스 많이느낌
                     when BP1 in (3, 4) then "2" --스트레그 적게느낌
                     end as BP1
              , case when HE_BMI<18.5 then "1" --저체중
                     when 18.5<=HE_BMI<23 then "2" --정상
                     when 23<=HE_BMI<25 then "3" --과체중
                     when 25<=HE_BMI then "4" --비만
                     end as HE_BMI
              , case when DL1_dg in (0, 1) then DL1_dg --0:무, 1:유
                     end as DL1_dg
FROM data
WHERE AGE >= 19
')

But then I got an error message

Error: near "then": syntax error

Any help will be appreciated.

project=sqldf('select distinct
              ID
              , sex  
              , case when age<30 then "1" --20대
                     when age<40 then "2" --30대
                     when age<50 then "3" --40대
                     when age<60 then "4" --50대
                     when age<70 then "5" --60대
                     when 70<=age then "6" --70대 
                     end as age
              , ho_incm
              , town_t
              , case when tins in (10, 20) then "1" --국민건강보험
                   when tins=30 then "2" --의료급여
                   end as tins
              , edu 
              , marri_1 in (1, 2) then marri_1 --1:기혼, 2:이혼
                   end as marri_1
              , case when EC1_1 in (1, 2) then EC1_1 --1:취업, 2:실업
                   end as EC1_1
              , case when BS3_1 in (1, 2) then "1" --현재흡연
                     when BS3_1=3 then "2" --과거흡연
                     when BS3_1=8 then "3" --비흡연
                     end as BS3_1
              , case when BD1_11 in (1, 2) then "2" --비음주
                     when BD1_11 in (3, 4, 5, 6) then "1" --음주
                     end as BD1_11
              , case when BP1 in (1, 2) then "1" --스트레스 많이느낌
                     when BP1 in (3, 4) then "2" --스트레그 적게느낌
                     end as BP1
              , case when HE_BMI<18.5 then "1" --저체중
                     when 18.5<=HE_BMI<23 then "2" --정상
                     when 23<=HE_BMI<25 then "3" --과체중
                     when 25<=HE_BMI then "4" --비만
                     end as HE_BMI
              , case when DL1_dg in (0, 1) then DL1_dg --0:무, 1:유
                     end as DL1_dg
FROM data
WHERE AGE >= 19
')
4b0
  • 21,981
  • 30
  • 95
  • 142
  • 1
    Welcome to StackOverflow. Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing your data, a portion of your data, or example data using `dput()`? – jrcalabrese Nov 27 '22 at 16:00
  • It looks as if your `marri_1` is missing the `case when` and such. – r2evans Nov 28 '22 at 10:30
  • The line after the `edu` line is the problem. – G. Grothendieck Nov 28 '22 at 20:03

0 Answers0