R Data Manipulation

R Data Manipulation

Now that you have data in R, how do you make changes to it? This page has simple, easy code to help you with R data manipulation.

Add a new column to a dataframe.

In the examples below the data frame is called “data”. You can see here that data frames and columns in R are specified by first listing the data frame, then the dollar sign, then the name of the column. Whatever is on the left of the <- sign “gets” whatever is on the right.

The new column can be filled with NA which is R’s way of indicating an empty cell:

data$newcolumn<-NA

The column can also be filled with a number:

data$newcolumn<-534

Or the column could be filled with text:

data$newcolumn<-"your text"

Add a new column to a data frame and copy the data from an existing column into it.

Or you could make a new column in your data frame and copy the data from an existing column:

data$newcolumn<-data$existingcolumn

Practice

To practice manipulating data in R, try the exercises in this manipulating data tutorial.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting!

JM

Leave a Reply

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