객체 <- 함수
객체가 함수로부터 생성된다.
setwd(‘경로 입력’)
setwd('C:/github/R/Discovering-Statistics-Using-R') getwd() # 현재 작업환경 보기
객체를 생성하고 주어진 여러 인수를 하나로 묶는 역할을 한다.
metallica <- c("Lars", "James", "Jason", "Kirk")
> metallica != "Jason"
[1] TRUE TRUE FALSE TRUE
> metallica[metallica != "Jason"]
[1] "Lars" "James" "Kirk"
> metallica = metallica[metallica != "Jason"]
> metallica
[1] "Lars" "James" "Kirk"
> metallica <- c(metallica, "Rob")
> metallica
[1] "Lars" "James" "Kirk" "Rob"
> myData <- read.delim("../data/data.dat")
> myData
name birth_date job friends alcohol income neurotic
1 Ben 7/3/1977 1 5 10 20000 10
2 Martin 5/24/1969 1 2 15 40000 17
3 Andy 6/21/1973 1 0 20 35000 14
4 Paul 7/16/1970 1 4 5 22000 13
5 Graham 10/10/1949 1 1 30 50000 21
6 Carina 11/5/1983 2 10 25 5000 7
7 Karina 10/8/1987 2 12 20 100 13
8 Doug 1/23/1989 2 15 16 3000 9
9 Mark 5/20/1973 2 12 17 10000 14
10 Zoe 11/12/1984 2 17 18 10 13
install.packages("DSUR") # DSUR 이라는 패키지 설치
library(DSUR)
car::recode() # car 패키지에 있는 recode()함수를 사용
> job<-c(1,1,1,1,1,2,2,2,2,2) # 기본적인 생성 방법
> job
[1] 1 1 1 1 1 2 2 2 2 2
> job<-c(rep(1, 5),rep(2, 5)) # 반복 사용
> job
[1] 1 1 1 1 1 2 2 2 2 2
> job<-factor(job, levels = c(1:2), labels = c("Lecturer", "Student")) # job 객체를 1 -> Lecturer, 2 -> Student 로 변환
> job
[1] Lecturer Lecturer Lecturer Lecturer Lecturer Student Student Student Student Student
Levels: Lecturer Student
> job<-gl(2, 5, labels = c("Lecturer", "Student")) # gl : general level, gl(수준 수, 각 수준의 사례 수, 전체 사례 수, labels = c("이름표1", "이름표2",...))
> job
[1] Lecturer Lecturer Lecturer Lecturer Lecturer Student Student Student Student Student
Levels: Lecturer Student
> metallicaAges <- c(47,47,48,46)
> metallicaNames <- metallica
> metallica <- data.frame(Name=metallicaNames, Age = metallicaAges)
> metallica
Name Age
1 Lars 47
2 James 47
3 Kirk 48
4 Rob 46
> metallica$Name
[1] Lars James Kirk Rob
Levels: James Kirk Lars Rob
> metallica$childAge<-c(12,12,4,6)
> metallica
Name Age childAge
1 Lars 47 12
2 James 47 12
3 Kirk 48 4
4 Rob 46 6
> names(metallica)
[1] "Name" "Age" "childAge"
> metallica$fatherhoodAge<-metallica$Age - metallica$childAge
> metallica
Name Age childAge fatherhoodAge
1 Lars 47 12 35
2 James 47 12 35
3 Kirk 48 4 44
4 Rob 46 6 40
> lecturerPersonality <- lecturerData[, c("friends", "alcohol", "neurotic")] # 일부 변수들만 보고 싶을 때
> lecturerPersonality
friends alcohol neurotic
1 5 10 10
2 2 15 17
3 0 20 14
4 4 5 13
5 1 30 21
6 10 25 7
7 12 20 13
8 15 16 9
9 12 17 14
10 17 18 13
> lecturerOnly <- lecturerData[job=="Lecturer",]
> lecturerOnly
name birth_date job friends alcohol income neurotic
1 Ben 7/3/1977 1 5 10 20000 10
2 Martin 5/24/1969 1 2 15 40000 17
3 Andy 6/21/1973 1 0 20 35000 14
4 Paul 7/16/1970 1 4 5 22000 13
5 Graham 10/10/1949 1 1 30 50000 21
> lecturerOnly <- subset(lecturerData, job=="Lecturer")
Discovering Statistics Using R - 010 [ 2024년12월25일 ]
Discovering Statistics Using R - 009 [ 2024년12월12일 ]
Discovering Statistics Using R - 008 [ 2024년12월10일 ]
Discovering Statistics Using R - 007 [ 2024년12월06일 ]
Discovering Statistics Using R - 006 [ 2024년12월05일 ]
Discovering Statistics Using R - 005 [ 2024년12월04일 ]
Discovering Statistics Using R - 004 [ 2024년12월03일 ]
Discovering Statistics Using R - 003 [ 2024년11월30일 ]