Open In App

How to create a new column in a tibble in R

In R Programming Language Tibbles is a tidy and user-friendly data structure. It's an efficient way to handle data. Adding a new column to a tibble is a basic yet essential task. Here we'll explore simple methods to accomplish this.

What is Tibbles?

A tibble is basically a sleeker version of a data frame in R. It's part of a package called tidyverse that makes working with data easier. Tibbles look and act a lot like data frames, but they have some extra features that make them easier to work with.

Syntax:

library(dplyr)

new_tibble <- old_tibble %>%

mutate(new_column_name = expression)

Create a new column in a tibble using mutate()

library(dplyr)

# Example data
employee_data <- tibble(
  employee_id = c(1, 2, 3),
  age = c(25, 30, 35),
  salary = c(50000, 60000, 70000)
)
#print the original table
print("Original Table:")
print(employee_data)

# Add a new column for salary bonus
employee_data <- employee_data %>%
  mutate(salary_bonus = salary * 0.1)

# View the updated tibble
print("New Table:")
print(employee_data)

Output:

[1] "Original Table:"
# A tibble: 3 × 3
employee_id age salary
<dbl> <dbl> <dbl>
1 1 25 50000
2 2 30 60000
3 3 35 70000

[1] "New Table:"
# A tibble: 3 × 4
employee_id age salary salary_bonus
<dbl> <dbl> <dbl> <dbl>
1 1 25 50000 5000
2 2 30 60000 6000
3 3 35 70000 7000

Create a new column in a tibble using the $-Operator

# Example data
employee_data <- tibble(
  employee_id = c(1, 2, 3),
  age = c(25, 30, 35),
  salary = c(50000, 60000, 70000)
)
#print the original table
print("Original Table:")
print(employee_data)

# Direct assignment to create a new column
employee_data$salary_bonus <- employee_data$salary * 0.1

print ("New Table:")
print(employee_data)

Output:

[1] "Original Table:"
# A tibble: 3 × 3
employee_id age salary
<dbl> <dbl> <dbl>
1 1 25 50000
2 2 30 60000
3 3 35 70000

[1] "New Table:"
# A tibble: 3 × 4
employee_id age salary salary_bonus
<dbl> <dbl> <dbl> <dbl>
1 1 25 50000 5000
2 2 30 60000 6000
3 3 35 70000 7000

Create a new column in a tibble using transform()

transform() is a base R function that creates a new data frame by adding or replacing columns.

# Example data
employee_data <- tibble(
  employee_id = c(1, 2, 3, 4, 5),
  age = c(25, 30, 40, 55, 28),
  salary = c(50000, 60000, 70000, 80000, 55000)
)

# Print the original table
print("Original Table:")
print(employee_data)

# Add a new column for age group using transform
employee_data <- transform(employee_data,
                           age_group = ifelse(age < 30, "Young",
                                           ifelse(age <= 50, "Middle-aged", "Senior")))

# Print the new table
print("New Table:")
print(employee_data)

Output:

[1] "Original Table:"
# A tibble: 5 × 3
employee_id age salary
<dbl> <dbl> <dbl>
1 1 25 50000
2 2 30 60000
3 3 40 70000
4 4 55 80000
5 5 28 55000

[1] "New Table:"
employee_id age salary age_group
1 1 25 50000 Young
2 2 30 60000 Middle-aged
3 3 40 70000 Middle-aged
4 4 55 80000 Senior
5 5 28 55000 Young

Create a new column in a tibble using Brackets []

# Example data
employee_data <- tibble(
  employee_id = c(1, 2, 3, 4, 5),
  age = c(25, 30, 40, 55, 28),
  salary = c(50000, 60000, 70000, 80000, 55000)
)

# Print the original table
print("Original Table:")
print(employee_data)

# Add a new column for department using brackets
employee_data["department"] <- c("HR", "Finance", "IT", "Marketing", "Operations")

# Print the new table
print("New Table:")
print(employee_data)

Output:

[1] "Original Table:"
# A tibble: 5 × 3
employee_id age salary
<dbl> <dbl> <dbl>
1 1 25 50000
2 2 30 60000
3 3 40 70000
4 4 55 80000
5 5 28 55000

[1] "New Table:"
# A tibble: 5 × 4
employee_id age salary department
<dbl> <dbl> <dbl> <chr>
1 1 25 50000 HR
2 2 30 60000 Finance
3 3 40 70000 IT
4 4 55 80000 Marketing
5 5 28 55000 Operations

Create a new column in a tibble using cbind()

# Example data
employee_data <- tibble(
  employee_id = c(1, 2, 3, 4, 5),
  age = c(25, 30, 40, 55, 28),
  salary = c(50000, 60000, 70000, 80000, 55000)
)

# Print the original table
print("Original Table:")
print(employee_data)

# Define values for the new column
gender <- c("Male", "Female", "Male", "Female", "Male")

# Add a new column for gender using cbind
employee_data <- cbind(employee_data, gender)

# Print the new table
print("New Table:")
print(employee_data)

Output:

[1] "Original Table:"
# A tibble: 5 × 3
employee_id age salary
<dbl> <dbl> <dbl>
1 1 25 50000
2 2 30 60000
3 3 40 70000
4 4 55 80000
5 5 28 55000


[1] "New Table:"

employee_id age salary gender
1 1 25 50000 Male
2 2 30 60000 Female
3 3 40 70000 Male
4 4 55 80000 Female
5 5 28 55000 Male

Conclusion

There are several easy methods to add a new column to a tibble in R. These include using functions like mutate(), direct assignment with the $ operator, transform(), brackets [], cbind(), and others. Each method offers flexibility, making it simple to organize and enhance the data. So, whether we're categorizing data, performing calculations, or simply organizing our dataset, these methods provide the tools needed to manipulate tibbles effectively in R.

Article Tags :