Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Creating and Modifying Variables | Section
Практика
Проекти
Вікторини та виклики
Вікторини
Виклики
/
Data Wrangling with Tidyverse in R

bookCreating and Modifying Variables

Свайпніть щоб показати меню

When you work with data, you often need to create new variables or modify existing ones to prepare your dataset for analysis. In the Tidyverse, the mutate and transmute functions from the dplyr package allow you to efficiently add, change, or remove columns in your data frame. These functions are essential tools for transforming raw data into a format that is ready for modeling, visualization, or reporting.

12345678910111213141516171819202122232425262728
library(dplyr) options(crayon.enabled = FALSE) # Sample data frame data <- tibble( name = c("Alice", "Bob", "Carol"), math_score = c(80, 90, 85), english_score = c(75, 88, 92) ) # Using mutate to add and transform columns data_mutated <- data %>% mutate( total_score = math_score + english_score, average_score = (math_score + english_score) / 2, math_score = math_score * 1.1 # increase math_score by 10% ) # Using transmute to create only new columns, dropping others data_transmuted <- data %>% transmute( name, total_score = math_score + english_score, average_score = (math_score + english_score) / 2 ) print(data_mutated) print(data_transmuted)
copy

The main difference between mutate and transmute is in what they return. When you use mutate, you add new columns or modify existing ones, but all other columns in the data frame remain. In contrast, transmute only keeps the columns you explicitly create or mention in your call; all other columns are dropped. Use mutate if you want to keep your original columns while adding or changing variables, and use transmute when you only want to keep the newly created variables or a specific subset of columns.

question mark

Which scenario is best suited for using transmute instead of mutate?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 9

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 9
some-alt