Skip Headers

SAS  SPSS  R-PROJECT  S-PLUS  PROC-SQL

통 계
프 로 그 램 비 교

Go to Documentation Home
HOME
Go to Book List
PRO_home
Go to Table of Contents
연구회
Go to Index
자료실
Go to Master Index
R-PROJECT
Go to Feedback page
MAIL

Go to previous page
Previous
Go to next page
Next

14. Value Labels Or Formats(& Measurement Level)


1. SAS
2. SPSS
3. R-PROJECT
4. S-PLUS
5. PROC SQL

 


1. SAS

MAIN

* 값 라벨(포맷)을 할당하기 위한 SAS프로그램;

PROC FORMAT;

     VALUE workshop_f 1="Control" 2="Treatment";

     VALUE $gender_f "m"="Male" "f"="Female";

     VALUE agreement 1='Strongly Disagree'

                     2='Disagree'

                     3='Neutral'

                     4='Agree'

                     5='Strongly Agree'.;

run;

 

DATA mydata;

 SET BACK.mydata;

     FORMAT workshop workshop_f. gender gender_f.

            q1-q4 agreement.;

run;

 

proc print;run;

OBS id workshop  gender q1                q2                       q3         q4

 1   1 Control   Female Strongly Disagree Strongly Disagree Strongly Agree.   Strongly Disagree

 2   2 Treatment Female Disagree          Strongly Disagree Agree             Strongly Disagree

 3   3 Control   Female Disagree          Disagree          Agree             Neutral

 4   4 Treatment Female Neutral           Strongly Disagree                 . Neutral

 5   5 Control   Male   Agree             Strongly Agree.   Disagree          Agree

 6   6 Treatment Male   Strongly Agree.   Agree             Strongly Agree.   Strongly Agree.

 7   7 Control   Male   Strongly Agree.   Neutral           Agree             Agree

 8   8 Treatment Male   Agree             Strongly Agree.   Strongly Agree.   Strongly Agree.



2. SPSS

MAIN

* 값 라벨을 할당하기 위한 SPSS 프로그램.

GET FILE="c:\mydata.sav".

VARIABLE LEVEL workshop (NOMINAL)

 /q1 TO q4 (SCALE).

VALUE LABELS  workshop 1 'Control'  2 'Treatment'

 /q1 TO q4

 1 'Strongly Disagree'

 2 'Disagree'

 3 'Neutral'

 4 'Agree'

 5 'Strongly Agree'.

SAVE OUTFILE="C:\mydata.sav".

 


3. R-PROJECT

MAIN

* 라벨과 Factor 상태를 할당하기 위한 R-Project 프로그램.

# 기본적으로, Group 수치형으로 읽혀지고, Gender Factor로써 읽혀진다.

# Gender 문자 이기 때문이다.

load(file="c:\\mydata.Rdata")

attach(mydata)

print(mydata)

 

# 기본적으로, Summary Group 수치형으로 취급하지만, Gender Factor 가정하고, 그것의 레벨을 카운트한다.

summary(mydata)

workshop   gender       q1             q2             q3              q4     

 Min.   :1.0   f:4    Min.   :1.00   Min.   :1.00   Min.   :2.000   Min.   :1.00 

 1st Qu.:1.0   m:4    1st Qu.:2.00   1st Qu.:1.00   1st Qu.:4.000   1st Qu.:2.50 

 Median :1.5          Median :3.50   Median :2.50   Median :4.000   Median :3.50 

 Mean   :1.5          Mean   :3.25   Mean   :2.75   Mean   :4.143   Mean   :3.25 

 3rd Qu.:2.0          3rd Qu.:4.25   3rd Qu.:4.25   3rd Qu.:5.000   3rd Qu.:4.25 

 Max.   :2.0          Max.   :5.00   Max.   :5.00   Max.   :5.000   Max.   :5.00

                                                    NA's   :1.000 

 

# Workshop변수를 Factor 변경.

mydata$workshop <- factor( mydata$workshop,

                           levels=c(1,2,3,4),

                           labels=c("R","SAS","SPSS","Stata") )

print(mydata)

workshop gender q1 q2 q3 q4

1        R      f  1  1  5  1

2      SAS      f  2  1  4  1

3        R      f  2  2  4  3

4      SAS      f  3  1 NA  3

5        R      m  4  5  2  4

6      SAS      m  5  4  5  5

7        R      m  5  3  4  4

8      SAS      m  4  5  5  5

 

# Summary함수는 workshop변수의 출현 횟수를 카운트한다.

# 현재의 workshop 평균은 잘못된 기록이다.

summary(mydata)

workshop gender       q1             q2             q3              q4     

 R    :4   f:4    Min.   :1.00   Min.   :1.00   Min.   :2.000   Min.   :1.00 

 SAS  :4   m:4    1st Qu.:2.00   1st Qu.:1.00   1st Qu.:4.000   1st Qu.:2.50 

 SPSS :0          Median :3.50   Median :2.50   Median :4.000   Median :3.50 

 Stata:0          Mean   :3.25   Mean   :2.75   Mean   :4.143   Mean   :3.25 

                  3rd Qu.:4.25   3rd Qu.:4.25   3rd Qu.:5.000   3rd Qu.:4.25 

                  Max.   :5.00   Max.   :5.00   Max.   :5.000   Max.   :5.00 

                                                NA's   :1.000

 

# Hmisc 패키지에서 Describe함수를 이용.

# Summary함수와 틀리게, Describe함수는 q변수의 빈도와 평균,백분율을 계산한다.

# Describe함수를 사용하기 위해서 Hmisc 라이브러리를 인스톨해야한다.

library(Hmisc)

describe(mydata)

mydata

 6  Variables      8  Observations

---------------------------------------------------------------------------

workshop

      n missing  unique

      8       0       2

R (4, 50%), SAS (4, 50%)

---------------------------------------------------------------------------

gender

      n missing  unique

      8       0       2

f (4, 50%), m (4, 50%)

---------------------------------------------------------------------------

q1

      n missing  unique    Mean

      8       0       5    3.25

           1  2  3  4  5

Frequency  1  2  1  2  2

%         12 25 12 25 25

---------------------------------------------------------------------------

q2

      n missing  unique    Mean

      8       0       5    2.75

           1  2  3  4  5

Frequency  3  1  1  1  2

%         38 12 12 12 25

---------------------------------------------------------------------------

q3

      n missing  unique    Mean

      7       1       3   4.143

2 (1, 14%), 4 (3, 43%), 5 (3, 43%)

---------------------------------------------------------------------------

q4

      n missing  unique    Mean

      8       0       4    3.25

1 (2, 25%), 3 (2, 25%), 4 (2, 25%), 5 (2, 25%)

---------------------------------------------------------------------------

 

# 어떻게 레벨이 값으로 매치되는지 확인.

unclass(mydata$gender)

[1] 1 1 1 1 2 2 2 2

attr(,"levels")

[1] "f" "m"

 

# m male f female 순서를 변경하자.

# 만약 값이 대문자이면, 실제적으로 결측값을 생성한다.

mydata$genderF <- factor( mydata$gender,

levels=c("m","f"),labels=c("male","female") )

workshop gender q1 q2 q3 q4 genderF

1        R      f  1  1  5  1  female

2      SAS      f  2  1  4  1  female

3        R      f  2  2  4  3  female

4      SAS      f  3  1 NA  3  female

5        R      m  4  5  2  4    male

6      SAS      m  5  4  5  5    male

7        R      m  5  3  4  4    male

8      SAS      m  4  5  5  5    male

 

# 매치된 결과를 확인 하기 위해서 Gender Genderf 출력.

print( mydata[ ,c("gender","genderF")] )

gender genderF

1      f  female

2      f  female

3      f  female

4      f  female

5      m    male

6      m    male

7      m    male

8      m    male

 

# 각각의 기초되는 값을 추출.

# genderNums 변수 값의 알파벳 순서가 할당된다.

# genderFNums 위에서 factor함수의 levels 순서에 따라서 m 2, f 1 할당된다.

mydata$genderNums  <- as.numeric(mydata$gender)

mydata$genderFNums <- as.numeric(mydata$genderF)

# 실제 할당된 값을 확인.

print(mydata)

workshop gender q1 q2 q3 q4 genderF genderNums genderFNums

1        R      f  1  1  5  1  female          1           2

2      SAS      f  2  1  4  1  female          1           2

3        R      f  2  2  4  3  female          1           2

4      SAS      f  3  1 NA  3  female          1           2

5        R      m  4  5  2  4    male          2           1

6      SAS      m  5  4  5  5    male          2           1

7        R      m  5  3  4  4    male          2           1

8      SAS      m  4  5  5  5    male          2           1

 

# Factor 이용하기 위해 q변수의 복사본을 생성하고, 그것을 카운트 할수 있다.

# 반복하여 사용하기 위해 라벨을 저장.

myQlevels <- c(1,2,3,4,5)

# 반복하여 이용하기 위해 라벨을 저장.

myQlabels <- c("Strongly Disagree",

            "Disagree",

               "Neutral",

               "Agree",

               "Strongly Agree")

 

# Factor함수를 이용하여 새로운 변수 세트를 생성.

mydata$q1f <- factor(q1, myQlevels, myQlabels)

mydata$q2f <- factor(q2, myQlevels, myQlabels)

mydata$q3f <- factor(q3, myQlevels, myQlabels)

mydata$q4f <- factor(q4, myQlevels, myQlabels)

workshop gender q1 q2 q3 q4 genderF genderNums genderFNums               q1f

1        R      f  1  1  5  1  female          1           2 Strongly Disagree

2      SAS      f  2  1  4  1  female          1           2          Disagree

3        R      f  2  2  4  3  female          1           2          Disagree

4      SAS      f  3  1 NA  3  female          1           2           Neutral

5        R      m  4  5  2  4    male          2           1             Agree

6      SAS      m  5  4  5  5    male          2           1    Strongly Agree

7        R      m  5  3  4  4    male          2           1    Strongly Agree

8      SAS      m  4  5  5  5    male          2           1             Agree

                q2f            q3f               q4f

1 Strongly Disagree Strongly Agree Strongly Disagree

2 Strongly Disagree          Agree Strongly Disagree

3          Disagree          Agree           Neutral

4 Strongly Disagree           <NA>           Neutral

5    Strongly Agree       Disagree             Agree

6             Agree Strongly Agree    Strongly Agree

7           Neutral          Agree             Agree

8    Strongly Agree Strongly Agree    Strongly Agree

 

# Summary함수 결과.

summary( mydata[ c("q1f","q2f","q3f","q4f") ] )

q1f                   q2f                   q3f  

 Strongly Disagree:1   Strongly Disagree:3   Strongly Disagree:0 

 Disagree         :2   Disagree         :1   Disagree         :1 

 Neutral          :1   Neutral          :1   Neutral          :0 

 Agree            :2   Agree            :1   Agree            :3 

 Strongly Agree   :2   Strongly Agree   :2   Strongly Agree   :3 

                                             NA's             :1 

                q4f  

 Strongly Disagree:2 

 Disagree         :0 

 Neutral          :2 

 Agree            :2 

 Strongly Agree   :2

 

#---Factor 이용하기 위해서 q변수의 복사번을 생성. 만약 변수 수가 많다면, 자동적으로 쉽게 할수 있는 방법.

# Factor로써 이용하기위해 q 변수의 복사본을 생성, 그결과 그것들을 카운트할수 있다.

myQlevels <- c(1,2,3,4,5)

myQlabels <- c("Strongly Disagree",

               "Disagree",

               "Neutral",

               "Agree",

               "Strongly Agree")

print(myQlevels)

[1] 1 2 3 4 5

 

print(myQlabels)

[1] "Strongly Disagree" "Disagree"          "Neutral"         

[4] "Agree"             "Strongly Agree"

 

# 이용될 변수 이름의 두개 세트를 생성.

myQnames  <- paste( "q",  1:4, sep="")

myQFnames <- paste( "qf", 1:4, sep="")

print(myQnames) # 변수명.

[1] "q1" "q2" "q3" "q4"

 

print(myQFnames)  # 새로운 factor 변수의 이름.

[1] "qf1" "qf2" "qf3" "qf4"

 

# 데이터 프레임을 분리하기 위해 q변수 추출.

myQFvars <- mydata[ ,myQnames]

print(myQFvars)

q1 q2 q3 q4

1  1  1  5  1

2  2  1  4  1

3  2  2  4  3

4  3  1 NA  3

5  4  5  2  4

6  5  4  5  5

7  5  3  4  4

8  4  5  5  5

 

# Factor 대하여 F 가진 모든 변수로 변수명을 변경.

names(myQFvars) <- myQFnames

print(myQFvars)

qf1 qf2 qf3 qf4

1   1   1   5   1

2   2   1   4   1

3   2   2   4   3

4   3   1  NA   3

5   4   5   2   4

6   5   4   5   5

7   5   3   4   4

8   4   5   5   5

 

# 많은 변수의 라벨을 적용하기 위해 함수 생성.

myLabeler <- function(x) { factor(x, myQlevels, myQlabels) }

# 변수가 함수로 어떻게 적용되는지 확인할수 있다.

summary( myLabeler(myQFvars["qf1"]) )

Strongly Disagree          Disagree           Neutral             Agree

                0                 0                 0                 0

   Strongly Agree              NA's

                0                 1

 

# 모든 변수에 적용.

myQFvars[ ,myQFnames] <-   lapply( myQFvars[ ,myQFnames ], myLabeler )

qf1               qf2            qf3               qf4

1 Strongly Disagree Strongly Disagree Strongly Agree Strongly Disagree

2          Disagree Strongly Disagree          Agree Strongly Disagree

3          Disagree          Disagree          Agree           Neutral

4           Neutral Strongly Disagree           <NA>           Neutral

5             Agree    Strongly Agree       Disagree             Agree

6    Strongly Agree             Agree Strongly Agree    Strongly Agree

7    Strongly Agree           Neutral          Agree             Agree

8             Agree    Strongly Agree Strongly Agree    Strongly Agree

 

# Summary함수의 결과.

summary(myQFvars)

qf1                   qf2                   qf3  

 Strongly Disagree:1   Strongly Disagree:3   Strongly Disagree:0 

 Disagree         :2   Disagree         :1   Disagree         :1  

 Neutral          :1   Neutral          :1   Neutral          :0 

 Agree            :2   Agree            :1   Agree            :3 

 Strongly Agree   :2   Strongly Agree   :2   Strongly Agree   :3 

                                             NA's             :1 

                qf4  

 Strongly Disagree:2 

 Disagree         :0 

 Neutral          :2 

 Agree            :2 

 Strongly Agree   :2

 

# Mydata 새로운 변수를 결합.

mydata<-cbind(mydata,myQFvars)

print(mydata)

workshop gender q1 q2 q3 q4 genderF genderNums genderFNums               q1f

1        R      f  1  1  5  1  female          1           2 Strongly Disagree

2      SAS      f  2  1  4  1  female          1           2          Disagree

3        R      f  2  2  4  3  female          1           2          Disagree

4      SAS      f  3  1 NA  3  female          1           2           Neutral

5        R      m  4  5  2  4    male          2           1             Agree

6      SAS      m  5  4  5  5    male          2           1    Strongly Agree

7        R      m  5  3  4  4    male          2           1    Strongly Agree

8      SAS      m  4  5  5  5    male          2           1             Agree

                q2f            q3f               q4f               qf1

1 Strongly Disagree Strongly Agree Strongly Disagree Strongly Disagree

2 Strongly Disagree          Agree Strongly Disagree          Disagree

3          Disagree          Agree           Neutral          Disagree

4 Strongly Disagree           <NA>           Neutral           Neutral

5    Strongly Agree       Disagree             Agree             Agree

6             Agree Strongly Agree    Strongly Agree    Strongly Agree

7           Neutral          Agree             Agree    Strongly Agree

8    Strongly Agree Strongly Agree    Strongly Agree             Agree

                qf2            qf3               qf4

1 Strongly Disagree Strongly Agree Strongly Disagree

2 Strongly Disagree          Agree Strongly Disagree

3          Disagree          Agree           Neutral

4 Strongly Disagree           <NA>           Neutral

5    Strongly Agree       Disagree             Agree

6             Agree Strongly Agree    Strongly Agree

7           Neutral          Agree             Agree

8    Strongly Agree Strongly Agree    Strongly Agree

 

 


4. S-PLUS

MAIN

 

 

# 기본적으로, Group 수치형으로 읽혀지고, Gender Factor로써 읽혀진다.

# Gender 문자 이기 때문이다.

mydata<-read.table ("c:/data/mydata.csv",header=TRUE,

                                sep=",",row.names="id")

print(mydata)

attach(mydata)

 

# 기본적으로, Summary Group 수치형으로 취급하지만, Gender Factor 가정하고, 그것의 레벨을 카운트한다.

summary(mydata)

workshop   gender       q1             q2             q3              q4     

 Min.   :1.0   f:4    Min.   :1.00   Min.   :1.00   Min.   :2.000   Min.   :1.00 

 1st Qu.:1.0   m:4    1st Qu.:2.00   1st Qu.:1.00   1st Qu.:4.000   1st Qu.:2.50 

 Median :1.5          Median :3.50   Median :2.50   Median :4.000   Median :3.50 

 Mean   :1.5          Mean   :3.25   Mean   :2.75   Mean   :4.143   Mean   :3.25 

 3rd Qu.:2.0          3rd Qu.:4.25   3rd Qu.:4.25   3rd Qu.:5.000   3rd Qu.:4.25 

 Max.   :2.0          Max.   :5.00   Max.   :5.00   Max.   :5.000   Max.   :5.00

                                                    NA's   :1.000 

 

# Workshop변수를 Factor 변경.

mydata$workshop <- factor( mydata$workshop,

                           levels=c(1,2,3,4),

                           labels=c("R","SAS","SPSS","Stata") )

print(mydata)

workshop gender q1 q2 q3 q4

1        R      f  1  1  5  1

2      SAS      f  2  1  4  1

3        R      f  2  2  4  3

4      SAS      f  3  1 NA  3

5        R      m  4  5  2  4

6      SAS      m  5  4  5  5

7        R      m  5  3  4  4

8      SAS      m  4  5  5  5

 

# Summary함수는 workshop변수의 출현 횟수를 카운트한다.

# 현재의 workshop 평균은 잘못된 기록이다.

summary(mydata)

workshop gender       q1             q2             q3              q4     

 R    :4   f:4    Min.   :1.00   Min.   :1.00   Min.   :2.000   Min.   :1.00 

 SAS  :4   m:4    1st Qu.:2.00   1st Qu.:1.00   1st Qu.:4.000   1st Qu.:2.50 

 SPSS :0          Median :3.50   Median :2.50   Median :4.000   Median :3.50 

 Stata:0          Mean   :3.25   Mean   :2.75   Mean   :4.143   Mean   :3.25 

                  3rd Qu.:4.25   3rd Qu.:4.25   3rd Qu.:5.000   3rd Qu.:4.25 

                  Max.   :5.00   Max.   :5.00   Max.   :5.000   Max.   :5.00 

                                                NA's   :1.000

 

# 어떻게 레벨이 값으로 매치되는지 확인.

unclass(mydata$gender)

[1] 1 1 1 1 2 2 2 2

attr(, "levels"):

[1] "f" "m"

attr(, "class"):

[1] "factor"

 

# m male f female 순서를 변경하자.

# 만약 값이 대문자이면, 실제적으로 결측값을 생성한다.

mydata$genderF <- factor( mydata$gender,

  levels=c("m","f"),labels=c("male","female") )

workshop gender q1 q2 q3 q4 genderF

1        R      f  1  1  5  1  female

2      SAS      f  2  1  4  1  female

3        R      f  2  2  4  3  female

4      SAS      f  3  1 NA  3  female

5        R      m  4  5  2  4    male

6      SAS      m  5  4  5  5    male

7        R      m  5  3  4  4    male

8      SAS      m  4  5  5  5    male

 

# 매치된 결과를 확인 하기 위해서 Gender Genderf 출력.

print( mydata[ ,c("gender","genderF")] )

gender genderF

1      f  female

2      f  female

3      f  female

4      f  female

5      m    male

6      m    male

7      m    male

8      m    male

 

# 각각의 기초되는 값을 추출.

# genderNums 변수 값의 알파벳 순서가 할당된다.

# genderFNums 위에서 factor함수의 levels 순서에 따라서 m 2, f 1 할당된다.

mydata$genderNums  <- as.numeric(mydata$gender)

mydata$genderFNums <- as.numeric(mydata$genderF)

 

# 실제 할당된 값을 확인.

print(mydata)

workshop gender q1 q2 q3 q4 genderF genderNums genderFNums

1        R      f  1  1  5  1  female          1           2

2      SAS      f  2  1  4  1  female          1           2

3        R      f  2  2  4  3  female          1           2

4      SAS      f  3  1 NA  3  female          1           2

5        R      m  4  5  2  4    male          2           1

6      SAS      m  5  4  5  5    male          2           1

7        R      m  5  3  4  4    male          2           1

8      SAS      m  4  5  5  5    male          2           1

 

# Factor 이용하기 위해 q변수의 복사본을 생성하고, 그것을 카운트 할수 있다.

# 반복하여 사용하기 위해 라벨을 저장.

myQlevels <- c(1,2,3,4,5)

 

# 반복하여 이용하기 위해 라벨을 저장.

myQlabels <- c("Strongly Disagree",

            "Disagree",

               "Neutral",

               "Agree",

               "Strongly Agree")

 

# Factor함수를 이용하여 새로운 변수 세트를 생성.

mydata$q1f <- factor(q1, myQlevels, myQlabels)

mydata$q2f <- factor(q2, myQlevels, myQlabels)

mydata$q3f <- factor(q3, myQlevels, myQlabels)

mydata$q4f <- factor(q4, myQlevels, myQlabels)

  workshop gender q1 q2 q3 q4 genderF genderNums genderFNums

1        R      f  1  1  5  1  female          1           2

2      SAS      f  2  1  4  1  female          1           2

3        R      f  2  2  4  3  female          1           2

4      SAS      f  3  1 NA  3  female          1           2

5        R      m  4  5  2  4    male          2           1

6      SAS      m  5  4  5  5    male          2           1

7        R      m  5  3  4  4    male          2           1

8      SAS      m  4  5  5  5    male          2           1

 

                q1f               q2f            q3f               q4f

1 Strongly Disagree Strongly Disagree Strongly Agree Strongly Disagree

2          Disagree Strongly Disagree          Agree Strongly Disagree

3          Disagree          Disagree          Agree           Neutral

4           Neutral Strongly Disagree             NA           Neutral

5             Agree    Strongly Agree       Disagree             Agree

6    Strongly Agree             Agree Strongly Agree    Strongly Agree

7    Strongly Agree           Neutral          Agree             Agree

8             Agree    Strongly Agree Strongly Agree    Strongly Agree

 

# Summary함수 결과.

summary( mydata[ c("q1f","q2f","q3f","q4f") ] )

q1f                   q2f                   q3f  

 Strongly Disagree:1   Strongly Disagree:3   Strongly Disagree:0 

          Disagree:2            Disagree:1            Disagree:1 

           Neutral:1             Neutral:1             Neutral:0 

             Agree:2               Agree:1               Agree:3 

    Strongly Agree:2      Strongly Agree:2      Strongly Agree:3 

                                                          NA's:1 

 

                 q4f  

 Strongly Disagree:2 

          Disagree:0 

           Neutral:2 

             Agree:2 

    Strongly Agree:2

 

#---Factor 이용하기 위해서 q변수의 복사번을 생성. 만약 변수 수가 많다면, 자동적으로 쉽게 할수 있는 방법.

# Factor로써 이용하기위해 q 변수의 복사본을 생성, 그결과 그것들을 카운트할수 있다.

myQlevels <- c(1,2,3,4,5)

myQlabels <- c("Strongly Disagree",

               "Disagree",

               "Neutral",

               "Agree",

               "Strongly Agree")

print(myQlevels)

[1] 1 2 3 4 5

 

print(myQlabels)

[1] "Strongly Disagree" "Disagree"          "Neutral"         

[4] "Agree"             "Strongly Agree"

 

# 이용될 변수 이름의 두개 세트를 생성.

myQnames  <- paste( "q",  1:4, sep="")

myQFnames <- paste( "qf", 1:4, sep="")

print(myQnames) # 변수명.

[1] "q1" "q2" "q3" "q4"

 

print(myQFnames)  # 새로운 factor 변수의 이름.

[1] "qf1" "qf2" "qf3" "qf4"

 

# 데이터 프레임을 분리하기 위해 q변수 추출.

myQFvars <- mydata[ ,myQnames]

print(myQFvars)

q1 q2 q3 q4

1  1  1  5  1

2  2  1  4  1

3  2  2  4  3

4  3  1 NA  3

5  4  5  2  4

6  5  4  5  5

7  5  3  4  4

8  4  5  5  5

 

# Factor 대하여 F 가진 모든 변수로 변수명을 변경.

names(myQFvars) <- myQFnames

print(myQFvars)

qf1 qf2 qf3 qf4

1   1   1   5   1

2   2   1   4   1

3   2   2   4   3

4   3   1  NA   3

5   4   5   2   4

6   5   4   5   5

7   5   3   4   4

8   4   5   5   5

 

# 많은 변수의 라벨을 적용하기 위해 함수 생성.

myLabeler <- function(x) { factor(x, myQlevels, myQlabels) }

 

# 변수가 함수로 어떻게 적용되는지 확인할수 있다.

summary( myLabeler(myQFvars["qf1"]) )

Strongly Disagree          Disagree           Neutral             Agree

                0                 0                 0                 0

   Strongly Agree              NA's

                0                 1

 

# 모든 변수에 적용.

myQFvars[ ,myQFnames] <-   lapply( myQFvars[ ,myQFnames ], myLabeler )

qf1               qf2            qf3               qf4

1 Strongly Disagree Strongly Disagree Strongly Agree Strongly Disagree

2          Disagree Strongly Disagree          Agree Strongly Disagree

3          Disagree          Disagree          Agree           Neutral

4           Neutral Strongly Disagree           <NA>           Neutral

5             Agree    Strongly Agree       Disagree             Agree

6    Strongly Agree             Agree Strongly Agree    Strongly Agree

7    Strongly Agree           Neutral          Agree             Agree

8             Agree    Strongly Agree Strongly Agree    Strongly Agree

 

# Summary함수의 결과.

summary(myQFvars)

qf1                   qf2                   qf3  

 Strongly Disagree:1   Strongly Disagree:3   Strongly Disagree:0 

 Disagree         :2   Disagree         :1   Disagree         :1  

 Neutral          :1   Neutral          :1   Neutral          :0 

 Agree            :2   Agree            :1   Agree            :3 

 Strongly Agree   :2   Strongly Agree   :2   Strongly Agree   :3 

                                             NA's             :1 

                qf4  

 Strongly Disagree:2 

 Disagree         :0 

 Neutral          :2 

 Agree            :2 

 Strongly Agree   :2

 

# Mydata 새로운 변수를 결합.

mydata<-cbind(mydata,myQFvars)

print(mydata)

workshop gender q1 q2 q3 q4 genderF genderNums genderFNums

1        R      f  1  1  5  1  female          1           2

2      SAS      f  2  1  4  1  female          1           2

3        R      f  2  2  4  3  female          1           2

4      SAS      f  3  1 NA  3  female          1           2

5        R      m  4  5  2  4    male          2           1

6      SAS      m  5  4  5  5    male          2           1

7        R      m  5  3  4  4    male          2           1

8      SAS      m  4  5  5  5    male          2           1

 

                q1f               q2f            q3f               q4f

1 Strongly Disagree Strongly Disagree Strongly Agree Strongly Disagree

2          Disagree Strongly Disagree          Agree Strongly Disagree

3          Disagree          Disagree          Agree           Neutral

4           Neutral Strongly Disagree             NA           Neutral

5             Agree    Strongly Agree       Disagree             Agree

6    Strongly Agree             Agree Strongly Agree    Strongly Agree

7    Strongly Agree           Neutral          Agree             Agree

8             Agree    Strongly Agree Strongly Agree    Strongly Agree

 

                qf1               qf2            qf3               qf4

1 Strongly Disagree Strongly Disagree Strongly Agree Strongly Disagree

2          Disagree Strongly Disagree          Agree Strongly Disagree

3          Disagree          Disagree          Agree           Neutral

4           Neutral Strongly Disagree             NA           Neutral

5             Agree    Strongly Agree       Disagree             Agree

6    Strongly Agree             Agree Strongly Agree    Strongly Agree

7    Strongly Agree           Neutral          Agree             Agree

8             Agree    Strongly Agree Strongly Agree    Strongly Agree

 

 


5. PROC SQL

MAIN

 

options linesize=150;

* SAS Program to Assign Value Labels (formats);

PROC FORMAT;

     VALUE workshop_f 1="Control" 2="Treatment";

     VALUE $gender_f "m"="Male" "f"="Female";

     VALUE agreement 1='Strongly Disagree'

                     2='Disagree'

                     3='Neutral'

                     4='Agree'

                     5='Strongly Agree'.;

run;

 

proc sql;

  select id,

         workshop format=workshop_f.,

         gender   format=$gender_f.  ,

         q1       format=agreement. ,

         q2       format=agreement. ,

         q3       format=agreement. ,

         q4       format=agreement.

  from   BACK.mydata;

quit;

id   workshop  gender                 q1                 q2                 q3                 q4

-------------------------------------------------------------------------------------------------

 1  Control    Female  Strongly Disagree  Strongly Disagree  Strongly Agree.    Strongly Disagree

 2  Treatment  Female  Disagree           Strongly Disagree  Agree              Strongly Disagree

 3  Control    Female  Disagree           Disagree           Agree              Neutral

 4  Treatment  Female  Neutral            Strongly Disagree                  .  Neutral

 5  Control    Male    Agree              Strongly Agree.    Disagree           Agree

 6  Treatment  Male    Strongly Agree.    Agree              Strongly Agree.    Strongly Agree.

 7  Control    Male    Strongly Agree.    Neutral            Agree              Agree

 8  Treatment  Male    Agree              Strongly Agree.    Strongly Agree.    Strongly Agree.