How To Add A New Variable To A Dataframe In R
In this brief tutorial, you volition learn how to add together a column to a dataframe in R. More specifically, you lot will larn 1) to add a column using base R (i.e., past using the $-operator and brackets, two) add a cavalcade using the add_column() function (i.e., from tibble), 3) add multiple columns, and iv) to add columns from one dataframe to another.
Note, when adding a column with tibble we are, as well, going to use the %>% operator which is office of dplyr. Notation, dplyr, likewise as tibble, has plenty of useful functions that, apart from enabling us to add columns, make it easy to remove a column by proper name from the R dataframe (e.g., using the select() function).
Outline
Get-go, before reading an example data set from an Excel file, you are going to get the reply to a couple of questions. 2d, we will accept a look at the prerequisites to follow this tutorial. Third, we will have a await at how to add a new column to a dataframe using first base of operations R and, so, using tibble and the add_column() role. In this section, using dplyr and add_column(), we will also have a quick expect at how we can add together an empty column. Note, we will too suspend a cavalcade based on other columns. Furthermore, we are going to acquire, in the two last sections, how to insert multiple columns to a dataframe using tibble.
Prerequisites
To follow this tutorial, in which we will acquit out a simple data manipulation task in R, you just demand to install dplyr and tibble if you lot desire to use the add_column() and mutate() functions equally well equally the %>% operator. However, if you lot want to read the instance data, you will likewise need to install the readr bundle.
It may be worth noting that all the mentioned packages are all part of the Tidyverse. This package comes packed with a lot of tools that can be used for cleaning data, visualizing data (east.g. to create a scatter plot in R with ggplot2).
How exercise I add together a column to a DataFrame in R?
To add a new cavalcade to a dataframe in R you tin use the $-operator. For case, to add together the column "NewColumn", y'all can practice like this: dataf$NewColumn <- Values. Now, this volition finer add your new variable to your dataset.
How exercise I add a column from 1 Dataframe to another in R?
To add a column from i dataframe to another you lot can use the $ operator. For example, if you lot want to add the cavalcade named "A" from the dataframe called "dfa" to the dataframe called "dfb" you can run the following code. dfb$A <- dfa$A. Adding multiple columns from i dataframe to another tin also be accomplished, of course.
In the adjacent section, nosotros are going to use the read_excel() office from the readr package. After this, we are going to employ R to add together a column to the created dataframe.
Example Data
Here's how to read a .xlsx file in R:
# Import readxl library(readxl) # Read information from .xlsx file dataf <- read_excel('./SimData/add_column.xlsx')
Lawmaking language: R ( r ) In the code clamper to a higher place, we imported the file add_column.xlsx. This file was downloaded to the same directory every bit the script. We tin obtain some data about the structure of the data using the str() function:
Before going to the next section it may be worth pointing out that it is possible to import data from other formats. For instance, you lot can meet a couple of tutorials roofing how to read information from SPSS, Stata, and SAS:
- How to Read and Write Stata (.dta) Files in R with Haven
- Reading SAS Files in R
- How to Read & Write SPSS Files in R Statistical Environment
Now that we accept some example information, to practice with, move on to the next section in which we volition learn how to add together a new column to a dataframe in base R.
Two Methods to Add a Column to a Dataframe in R (Base of operations).
Beginning, nosotros will use the $-operator and assign a new variable to our dataset. Second, we will use brackets ("[ ]") to do the same.
1) Add a Column Using the $-Operator
Hither's how to add a new column to a dataframe using the $-operator in R:
# add column to dataframe dataf$Added_Column <- "Value"
Code language: R ( r ) Note how we used the operator $ to create the new cavalcade in the dataframe. What we added, to the dataframe, was a graphic symbol (i.due east., the same word). This will produce a grapheme vector equally long as the number of rows. Here'due south the first 6 rows of the dataframe with the added column:
If we, on the other mitt, tried to assign a vector that is non of the aforementioned length every bit the dataframe, it would neglect. Nosotros would go an error similar to "Mistake: Assigned data `c(2, 1)` must be compatible with existing data." For more about the dollar sign operator, check the post "How to use $ in R: 6 Examples – listing & dataframe (dollar sign operator)".
If we would like to add together a sequence of numbers nosotros tin can apply seq() function and the length.out argument:
# add column to dataframe dataf$Seq_Col <- seq(1, 10, length.out = dim(dataf)[1])
Code linguistic communication: R ( r )
Notice how we also used the dim() office and selected the first element (the number of rows) to create a sequence with the same length as the number of rows. Of course, in a real-life example, nosotros would probably want to specify the sequence a bit more earlier adding it every bit a new cavalcade. In the adjacent department, nosotros volition learn how to add a new column using brackets.
2) Add a Column Using Brackets ("[]")
Here's how to append a cavalcade to a dataframe in R using brackets ("[]"):
# Adding a new cavalcade dataf["Added_Column"] <- "Value"
Code language: R ( r ) Using the brackets will requite us the same consequence as using the $-operator. However, it may exist easier to utilize the brackets instead of $, sometimes. For example, when we have column names containing whitespaces, brackets may be the fashion to go. Besides, when selecting multiple columns you have to use brackets and not $. In the next section, we are going to create a new column by using tibble and the add_column() office.
How to Add a Column to a dataframe in R using the add_column() Function
Here'southward how to add a column to a dataframe in R:
# Append column using Tibble: dataf <- dataf %>% add_column(Add_Column = "Value")
Code language: R ( r ) In the instance higher up, we added a new cavalcade at "the end" of the dataframe. Note, that we tin can use dplyr to remove columns by name. This was done to produce the post-obit output:
Finally, if we want to, we can add a column and create a copy of our old dataframe. Change the code and then that the left "dataf" is something else eastward.g. "dataf2". Now, that nosotros have added a column to the dataframe information technology might exist time for other data manipulation tasks. For example, we may now want to remove indistinguishable rows from the R dataframe or transpose your dataframe.
Example 1: Add a New Cavalcade Later on Another Column
If we want to append a column at a specific position we tin can use the .after statement:
# R add column after another column dataf <- dataf %>% add_column(Column_After = "After", .afterward = "A")
Lawmaking linguistic communication: R ( r )
Every bit you probably understand, doing this volition add together the new cavalcade after the column "A". In the next example, nosotros are going to append a column before a specified column.
Case ii: Add a Cavalcade Before Some other Column
Here's how to add a column to the dataframe earlier another column:
# R add cavalcade before another column dataf <- dataf %>% add_column(Column_Before = "Before", .later = "Cost")
Code language: R ( r ) In the next case, we are going to use add_column() to add an empty cavalcade to the dataframe.
Example iii: Add together an Empty Column to the Dataframe
Hither'south how nosotros would practise if we wanted to add an empty cavalcade in R:
Note that we but added NA (missing value indicator) as the empty cavalcade. Here's the output, with the empty column, added, to the dataframe:
# Empty dataf <- dataf %>% add_column(Empty_Column = NA) %>%
Code language: R ( r )
If we desire to exercise this we just replace the NA with "''", for example. However, this would create a character column and may non exist considered empty. In the next instance, we are going to add together a column to a dataframe based on other columns.
Example iv: Add a Cavalcade Based on Other Columns (Conditionally)
Here'due south how to use R to add a column to a dataframe based on other columns:
# Append column conditionally dataf <- dataf %>% add_column(C = if_else(.$A == .$B, TRUE, Fake))
Code language: R ( r ) In the code clamper to a higher place, we added something to the add_column() function: the if_else() function. We did this because we wanted to add a value in the column based on the value in another column. Furthermore, we used the .$ then that we get the two columns compared (using ==). If the values in these two columns are the same nosotros add together True on the specific row. Here's the new column added:
Notation, you can also piece of work with the mutate() function (also from dplyr) to add columns based on atmospheric condition. See this tutorial for more information about calculation columns on the ground of other columns.
In the next section, we will have a look at how to work with the mutate() role to compute, and add together a new variable to the dataset.
Compute and Add a New Variable to a Dataframe in R with mutate()
Here's how to compute and add a new variable (i.e., column) to a dataframe in R:
# insert new column with mutate dataf <- dataf %>% mutate(DepressionIndex = hateful(c_across(Depr1:Depr5))) %>% head()
Lawmaking language: R ( r ) Discover how we, in the instance code above, calculated a new variable chosen "depression index" which was the mean of the v columns named Depr1 to Depr5. Obviously, we used the mean() function to summate the mean of the columns. Notice how we also used the c_across() function. This was done so that we can calculate the hateful beyond these columns.
Note now that you have added new columns, to the dataframe, you may likewise want to rename cistron levels in R with e.1000. dplyr. In the side by side section, however, we will add multiple columns to a dataframe.
How to Add Multiple Columns to the Dataframe in R
Hither'southward how y'all would insert multiple columns, to the dataframe, using the add_column() office:
# Add multiple columns dataf <- %>% add_column(New_Column1 = "1st Column Added", New_Column2 = "second Cavalcade Added")
Lawmaking language: R ( r ) In the case lawmaking above, we had two vectors ("a" and "b"). At present, we then used the add_column() method to append these two columns to the dataframe. Here'due south the first 6 rows of the dataframe with added columns:
Note, if yous desire to add multiple columns, you lot simply add an argument equally we did above for each cavalcade you want to insert. It is, again, of import that the length of the vector is the same equally the number of rows in the dataframe. Or else, we will end upwards with an error. Note, a more realistic example tin can exist that we want to have the absolute value in R (from e.g. one column) and add information technology to a new cavalcade. In the next instance, however, we will add columns from one dataframe to some other.
Add Columns from One Dataframe to Another Dataframe
In this department, you will acquire how to add columns from one dataframe to another. Here'south how you append e.1000. two columns from one dataframe to some other:
# Read information from the .xlsx files: dataf <- read_excel('./SimData/add_column.xlsx') dataf2 <- read_excel('./SimData/add_column2.xlsx') # Add the columns from the second dataframe to the commencement dataf3 <- cbind(dataf, dataf2[c("Anx1", "Anx2", "Anx3")])
Code linguistic communication: R ( r )
In the case above, we used the cbind() office together with selecting which columns we wanted to add. Note, that dplyr has the bind_cols() function that can be used in a like fashion. Now that you take put together your data sets you tin create dummy variables in R with east.g. the fastDummies package or calculate descriptive statistics.
Conclusion
In this postal service, y'all have learned how to add a cavalcade to a dataframe in R. Specifically, you have learned how to use the base functions bachelor, besides as the add_column() office from Tibble. Furthermore, y'all have learned how to apply the mutate() function from dplyr to append a column. Finally, you have likewise learned how to add multiple columns and how to add together columns from one dataframe to another.
I promise y'all learned something valuable. If you did, please share the tutorial on your social media accounts, add a link to it in your projects, or merely leave a comment below! Finally, suggestions and corrections are welcomed, also as comments beneath.
Other R Tutorials
Hither you volition notice some additional resources that y'all may observe useful- The first iii, hither, is especially interesting if you work with datetime objects (e.m., time-series data):
- How to Excerpt Year from Appointment in R with Examples with e.1000. lubridate (Tidyverse)
- Learn How to Extract Day from Datetime in R with Examples with east.g. lubridate (Tidyverse)
- How to Extract Time from Datetime in R – with Examples
If you are interested in other useful functions and/or operators these two posts might be useful:
- How to employ %in% in R: vii Case Uses of the Operator
- How to use the Repeat and Replicate functions in R
- How to Create a Matrix in R with Examples – empty, zeros
How To Add A New Variable To A Dataframe In R,
Source: https://www.marsja.se/how-to-add-a-column-to-dataframe-in-r-with-tibble-dplyr/#:~:text=To%20add%20a%20new%20column%20to%20a%20dataframe%20in%20R,new%20variable%20to%20your%20dataset.
Posted by: mizerruchoculd1984.blogspot.com

0 Response to "How To Add A New Variable To A Dataframe In R"
Post a Comment