Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • Check whether any values of a logical vector are TRUE
    Check whether any values of a logical vector are TRUE R
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • How to change the column positions in R?
    How to change the column positions in R? R
  • Arrange Data by Month in R
    Arrange Data by Month in R with example R
How to add columns to a data frame in R

How to add columns to a data frame in R

Posted on June 5June 4 By Jim No Comments on How to add columns to a data frame in R
Tweet
Share
Share
Pin

How to add columns to a data frame in R?, To add one or more columns to a data frame in R, use the mutate() function from the dplyr package.

With the following data frame, the following examples demonstrate how to use this syntax in practice.

How to add labels at the end of each line in ggplot2? (datasciencetut.com)

How to add columns to a data frame in R

Let’s create a data frame

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'),
                 points = c(122, 144, 154, 155, 120, 218, 229),
                 assists = c(43, 55, 77, 18, 114, NA,29))

Now we can view the data frame

df
    player points assists
1     P1    122      43
2     P2    144      55
3     P3    154      77
4     P4    155      18
5     P5    120     114
6     P6    218      NA
7     P7    229      29

Approach 1: Add Column at End of Data Frame

The following code demonstrates how to add a column to the data frame’s end.

Two Sample Proportions test in R-Complete Guide – Data Science Tutorials

at the end of the data frame, add a ‘blocks’ column

df <- df %>%
  mutate(score=c(1, 3, 3, 2, 4, 3, 6))

Let’s view the data frame

df
   player points assists score
1     P1    122      43     1
2     P2    144      55     3
3     P3    154      77     3
4     P4    155      18     2
5     P5    120     114     4
6     P6    218      NA     3
7     P7    229      29     6

It’s worth noting that you may create an empty column by just giving NA to each of the new column’s values.

Dealing With Missing values in R – Data Science Tutorials

at the conclusion of the data frame, add an empty column

df <- df %>%
        mutate(blocks=NA)
df
    player points assists score
1     P1    122      43    NA
2     P2    144      55    NA
3     P3    154      77    NA
4     P4    155      18    NA
5     P5    120     114    NA
6     P6    218      NA    NA
7     P7    229      29    NA

Approach 2: Add Column Before Specific Column

The following code demonstrates how to insert a column in front of a certain column in a data frame:

insert the ‘score’ column after the ‘points’ column.

df <- df %>%
        mutate(score=c(1, 3, 3, 2, 4, 3, 6),
              .before=points)
df
   player points assists score
1     P1    122      43     1
2     P2    144      55     3
3     P3    154      77     3
4     P4    155      18     2
5     P5    120     114     4
6     P6    218      NA     3
7     P7    229      29     6

Approach 3: Add Column After Specific Column

The following code demonstrates how to add a column to the data frame after a certain column.

How to Remove Columns from a data frame in R – Data Science Tutorials

Following the ‘points’ column, add a ‘score’ column

df <- df %>%
        mutate(blocks=c(1, 3, 3, 2, 4, 3, 6),
              .after=points)
df
player points blocks assists score
1     P1    122      1      43     1
2     P2    144      3      55     3
3     P3    154      3      77     3
4     P4    155      2      18     2
5     P5    120      4     114     4
6     P6    218      3      NA     3
7     P7    229      6      29     6

Approach 4: Add Column Based on Other Columns

The following code demonstrates how to add a column in a data frame based on another column.

Best GGPlot Themes You Should Know – Data Science Tutorials

add a ‘status’ column whose values are based on the ‘points’ column’s value

df <- df %>%
        mutate(status= if_else(.$points > 20, 'Good', 'Bad'))
df
   player points blocks assists score status
1     P1    122      1      43     1   Good
2     P2    144      3      55     3   Good
3     P3    154      3      77     3   Good
4     P4    155      2      18     2   Good
5     P5    120      4     114     4   Good
6     P6    218      3      NA     3   Good
7     P7    229      6      29     6   Good

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: How to Remove Columns from a data frame in R
Next Post: Count Observations by Group in R

Related Posts

  • How do confidence intervals work
    How do confidence intervals work? R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • How to create a heatmap in R
    How to create a heatmap in R R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • How to get the last value of each group in R
    How to get the last value of each group in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • Checking Missing Values in R
    Checking Missing Values in R R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme