<How to new variables>
>library(MASS)
>View(painters)
WAY1
EXAMPLE 1)
> painters$Drawing.ind=painters$Drawing>=mean(painters$Drawing)
> View(painters)
#You can see that there is a new variable Drawing.ind
> head(painters)
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 16 4 14 A
Drawing.ind
Da Udine FALSE
Da Vinci TRUE
Del Piombo TRUE
Del Sarto TRUE
Fr. Penni TRUE
Guilio Romano TRUE
EXAMPLE 2)
In R, the numeric value of 'TRUE' is 1 and 'FALSE' is 0. So if it is smaller than 10 than the new variable becomes 1. If Composition variable of painters is equal or greater than 10 and smaller than 13, the new variable will be represented as 2. And finally if it is greater or equal to 13, it becomes the biggest value which is 3.
>painters$Composition.Ord = 1 + (painters$Composition >= 10) + (painters$Composition >= 13)
> painters$Composition.Ord
[1] 2 3 1 2 1 3 1 3 1 3 2 3 2 3 3 2 3 3 2 3 2 3 1 1 1 1 2 1 1
[30] 3 2 3 3 1 3 3 3 3 3 1 1 1 1 2 2 3 3 3 3 3 2 3 3 3
> head(painters)
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 6 4 14 A
Drawing.ind Composition.Ord
Da Udine FALSE 2
Da Vinci TRUE 3
Del Piombo TRUE 1
Del Sarto TRUE 2
Fr. Penni TRUE 1
Guilio Romano TRUE 3
WAY2 (Using 'Transform')
>painters.new=transform(painters, Drawing.Ind=Drawing>=mean(Drawing))
> head(painters.new)
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 16 4 14 A
Drawing.ind Composition.Ord Drawing.Ind
Da Udine FALSE 2 FALSE
Da Vinci TRUE 3 TRUE
Del Piombo TRUE 1 TRUE
Del Sarto TRUE 2 TRUE
Fr. Penni TRUE 1 TRUE
Guilio Romano TRUE 3 TRUE
>School.Ord = ifelse(is.element(painters$School,c("A","B","C")),1,ifelse(is.element(painters$School,c("D","E")),2,3))
>table(School.Ord)
1 2 3
22 17 15
<Make a quantile value in several ways as an example>
> painters$Colour
[1] 16 4 16 9 8 4 4 7 10 12 8 8 6 7 10 5 6 12 6
[20] 9 0 12 17 14 18 15 14 16 17 16 18 16 10 16 15 9 10 10
[39] 13 10 16 6 6 14 16 10 17 17 13 17 8 8 4 6
> qt = quantile(painters$Colour, c(0, 0.25, 0.5, 0.75, 1))
> qt
0% 25% 50% 75% 100%
0.00 7.25 10.00 16.00 18.00
Way 1
> painters$colq_1<-1+(painters$Colour>7.25)+(painters$Colour>10)+(painters$Colour>16)
> painters$colq_1
[1] 3 1 3 2 2 1 1 1 2 3 2 2 1 1 2 1 1 3 1 2 1 3 4 3 4 3 3 3 4
[30] 3 4 3 2 3 3 2 2 2 3 2 3 1 1 3 3 2 4 4 3 4 2 2 1 1
> table(painters$colq_1)
1 2 3 4
14 15 18 7
> head(painters)
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 16 4 14 A
colq_1
Da Udine 3
Da Vinci 1
Del Piombo 3
Del Sarto 2
Fr. Penni 2
Guilio Romano 1
Way 2 (Using Ifelse)
> painters$col1_2=ifelse(painters$Colour<=7.25,1,ifelse(painters$Colour<=10,2,ifelse(painters$Colour<=16,3,4)))
> painters$col1_2
[1] 3 1 3 2 2 1 1 1 2 3 2 2 1 1 2 1 1 3 1 2 1 3 4 3 4 3 3 3 4
[30] 3 4 3 2 3 3 2 2 2 3 2 3 1 1 3 3 2 4 4 3 4 2 2 1 1
> table(painters$col1_2)
1 2 3 4
14 15 18 7
> head(painters)
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 16 4 14 A
colq_1 col1_2
Da Udine 3 3
Da Vinci 1 1
Del Piombo 3 3
Del Sarto 2 2
Fr. Penni 2 2
Guilio Romano 1 1
Way 3 (Using Cut )
> qt = quantile(painters$Colour, c(0, 0.25, 0.5, 0.75, 1))
> qt
0% 25% 50% 75% 100%
0.00 7.25 10.00 16.00 18.00
> painters$col_3 = cut(painters$Colour, breaks = qt, labels = c("first", "second", "third", "fourth"), include.lowest = T)
> painters$col_3
[1] third first third second second first first first
[9] second third second second first first second first
[17] first third first second first third fourth third
[25] fourth third third third fourth third fourth third
[33] second third third second second second third second
[41] third first first third third second fourth fourth
[49] third fourth second second first first
Levels: first second third fourth
> table(painters$col_3)
first second third fourth
14 15 18 7
> head(painters)
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 16 4 14 A
colq_1 col1_2 col_3
Da Udine 3 3 third
Da Vinci 1 1 first
Del Piombo 3 3 third
Del Sarto 2 2 second
Fr. Penni 2 2 second
Guilio Romano 1 1 first
'R' 카테고리의 다른 글
(R) reorganize data frames - stack, unstack, reshape function (0) | 2020.11.07 |
---|---|
(R) Combining Data frame, matrix and vectors using rbind, cbind / Merging data frames using merge() (0) | 2020.11.07 |
(R) Extract rows and columns from data frame with conditions (0) | 2020.11.05 |
(R)Wilkinson dot plot (+boxplot) (0) | 2020.10.29 |
(R) Two sample t-test (Student's t, Welch's t ) (0) | 2020.10.27 |