R'de yeni değişkenler oluşturmak için mutate nasıl kullanılır?


Bu eğitimde, bir veri çerçevesine yeni değişkenler eklemek için R’deki mutate() fonksiyonunun nasıl kullanılacağı açıklanmaktadır.

R’ye yeni değişkenler ekleme

Bir veri çerçevesine yeni değişkenler eklemek için aşağıdaki dplyr kitaplığı işlevleri kullanılabilir:

mutate() – mevcut değişkenleri korurken veri çerçevesine yeni değişkenler ekler

transmute() – bir veri çerçevesine yeni değişkenler ekler ve mevcut değişkenleri kaldırır

mutate_all() – bir veri çerçevesindeki tüm değişkenleri aynı anda değiştirir

mutate_at() – belirli değişkenleri ada göre değiştirir

mutate_if() – belirli bir koşulu karşılayan tüm değişkenleri değiştirir

mutasyona uğrama()

Mutate() işlevi, mevcut tüm değişkenleri korurken bir veri çerçevesine yeni değişkenler ekler. mutate() fonksiyonunun temel sözdizimi şöyledir:

 data <- mutate (new_variable = existing_variable/3)
  • veri: yeni değişkenlerin atanacağı yeni veri bloğu
  • new_variable: yeni değişkenin adı
  • mevcut_değişken: yeni değişkeni oluşturmak için üzerinde işlem yapmak istediğiniz veri çerçevesindeki mevcut değişken

Örneğin, aşağıdaki kod, gömülü iris veri kümesine yeni bir root_sepal_width değişkeninin nasıl ekleneceğini gösterir:

 #define data frame as the first six lines of the iris dataset
data <- head(iris)

#view data
data

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 5.0 3.6 1.4 0.2 setosa
#6 5.4 3.9 1.7 0.4 setosa

#load dplyr library
library(dplyr)

#define new column root_sepal_width as the square root of the Sepal.Width variable
data %>% mutate (root_sepal_width = sqrt(Sepal.Width))

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species root_sepal_width
#1 5.1 3.5 1.4 0.2 setosa 1.870829
#2 4.9 3.0 1.4 0.2 setosa 1.732051
#3 4.7 3.2 1.3 0.2 setosa 1.788854
#4 4.6 3.1 1.5 0.2 setosa 1.760682
#5 5.0 3.6 1.4 0.2 setosa 1.897367
#6 5.4 3.9 1.7 0.4 setosa 1.974842

dönüştürme()

transmute() işlevi, bir veri çerçevesine yeni değişkenler ekler ve mevcut değişkenleri kaldırır. Aşağıdaki kod, bir veri kümesine iki yeni değişkenin nasıl ekleneceğini ve mevcut tüm değişkenlerin nasıl kaldırılacağını gösterir:

 #define data frame as the first six lines of the iris dataset
data <- head(iris)

#viewdata
data

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 5.0 3.6 1.4 0.2 setosa
#6 5.4 3.9 1.7 0.4 setosa

#define two new variables and remove all existing variables
data %>% transmute (root_sepal_width = sqrt(Sepal.Width),
                   root_petal_width = sqrt(Petal.Width))

# root_sepal_width root_petal_width
#1 1.870829 0.4472136
#2 1.732051 0.4472136
#3 1.788854 0.4472136
#4 1.760682 0.4472136
#5 1.897367 0.4472136
#6 1.974842 0.6324555

mutasyon_all()

mutate_all() işlevi, bir veri çerçevesindeki tüm değişkenleri aynı anda değiştirerek, funs() işlevini kullanarak tüm değişkenler üzerinde belirli bir işlevi gerçekleştirmenize olanak tanır. Aşağıdaki kod, mutate_all() kullanılarak bir veri çerçevesindeki tüm sütunların 10’a nasıl bölüneceğini gösterir:

 #define new data frame as the first six rows of iris without the Species variable
data2 <- head(iris) %>% select(-Species)

#view the new data frame
data2

# Sepal.Length Sepal.Width Petal.Length Petal.Width
#1 5.1 3.5 1.4 0.2
#2 4.9 3.0 1.4 0.2
#3 4.7 3.2 1.3 0.2
#4 4.6 3.1 1.5 0.2
#5 5.0 3.6 1.4 0.2
#6 5.4 3.9 1.7 0.4

#divide all variables in the data frame by 10
data2 %>% mutate_all (funs(./10))

# Sepal.Length Sepal.Width Petal.Length Petal.Width
#1 0.51 0.35 0.14 0.02
#2 0.49 0.30 0.14 0.02
#3 0.47 0.32 0.13 0.02
#4 0.46 0.31 0.15 0.02
#5 0.50 0.36 0.14 0.02
#6 0.54 0.39 0.17 0.04

Eski değişken adına eklenecek yeni bir ad belirtilerek veri çerçevesine ek değişkenlerin eklenebileceğini unutmayın:

 data2 %>% mutate_all (funs(mod = ./10))

# Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length_mod
#1 5.1 3.5 1.4 0.2 0.51
#2 4.9 3.0 1.4 0.2 0.49
#3 4.7 3.2 1.3 0.2 0.47
#4 4.6 3.1 1.5 0.2 0.46
#5 5.0 3.6 1.4 0.2 0.50
#6 5.4 3.9 1.7 0.4 0.54
# Sepal.Width_mod Petal.Length_mod Petal.Width_mod
#1 0.35 0.14 0.02
#2 0.30 0.14 0.02
#3 0.32 0.13 0.02
#4 0.31 0.15 0.02
#5 0.36 0.14 0.02
#6 0.39 0.17 0.04

mutasyon_at()

mutate_at() işlevi belirli değişkenleri ada göre değiştirir. Aşağıdaki kod, mutate_at() kullanılarak iki belirli değişkenin 10’a nasıl bölüneceğini gösterir:

 data2 %>% mutate_at (c("Sepal.Length", "Sepal.Width"), funs(mod = ./10))

# Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length_mod
#1 5.1 3.5 1.4 0.2 0.51
#2 4.9 3.0 1.4 0.2 0.49
#3 4.7 3.2 1.3 0.2 0.47
#4 4.6 3.1 1.5 0.2 0.46
#5 5.0 3.6 1.4 0.2 0.50
#6 5.4 3.9 1.7 0.4 0.54
# Sepal.Width_mod
#1 0.35
#2 0.30
#3 0.32
#4 0.31
#5 0.36
#6 0.39

mutasyon_if()

mutate_if() işlevi, belirli bir koşulu karşılayan tüm değişkenleri değiştirir. Aşağıdaki kod, tür faktöründeki herhangi bir değişkeni tür karakterine dönüştürmek için mutate_if() işlevinin nasıl kullanılacağını gösterir:

 #find variable type of each variable in a data frame
data <- head(iris)
sapply(data, class)

#Sepal.Length Sepal.Width Petal.Length Petal.Width Species 
# "numeric" "numeric" "numeric" "numeric" "factor" 

#convert any variable of type factor to type character
new_data <- data %>% mutate_if(is.factor, as.character)
sapply(new_data, class)

#Sepal.Length Sepal.Width Petal.Length Petal.Width Species 
# "numeric" "numeric" "numeric" "numeric" "character"

Aşağıdaki kod, tüm sayısal değişkenleri tek bir ondalık basamağa yuvarlamak için mutate_if() işlevinin nasıl kullanılacağını gösterir:

 #define data as first six rows of iris dataset
data <- head(iris)

#view data
data

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 5.0 3.6 1.4 0.2 setosa
#6 5.4 3.9 1.7 0.4 setosa

#round any variables of type numeric to one decimal place
data %>% mutate_if(is.numeric, round, digits = 0)

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5 4 1 0 setosa
#2 5 3 1 0 setosa
#3 5 3 1 0 setosa
#4 5 3 2 0 setosa
#5 5 4 1 0 setosa
#6 5 4 2 0 setosa

Daha fazla okuma:
R’de application(), lapply(), sapply() ve tapply() için bir kılavuz
R’de çizgiler nasıl düzenlenir
R’deki satırlar nasıl filtrelenir

Yorum ekle

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir