- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add columns with square of each column in R data frame?
To add columns with square of each column in R data frame, we can use setNames function and cbind function for squaring each value. This might be required when we want to use squared form of variables in the data analysis.
Check out the below given examples to understand how it can be done.
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,2) x2<-rpois(20,5) x3<-rpois(20,1) df1<-data.frame(x1,x2,x3) df1
Output
The following dataframe is created −
x1 x2 x3 1 2 7 1 2 3 6 1 3 4 5 2 4 1 2 0 5 0 6 0 6 2 3 1 7 1 4 2 8 2 4 1 9 1 3 0 10 2 3 1 11 1 5 2 12 3 3 1 13 3 4 0 14 1 7 1 15 3 3 3 16 2 5 0 17 2 8 2 18 0 5 3 19 6 1 0 20 4 4 3
To add squared columns in df1, add the following code to the above snippet −
x1<-rpois(20,2) x2<-rpois(20,5) x3<-rpois(20,1) df1<-data.frame(x1,x2,x3) setNames(as.data.frame(cbind(df1,df1^2)), c(names(df1),paste0(names(df1),'_2')))
Output
If you execute all the above given codes as a single program, it generates the following output −
x1 x2 x3 x1_2 x2_2 x3_2 1 2 7 1 4 49 1 2 3 6 1 9 36 1 3 4 5 2 16 25 4 4 1 2 0 1 4 0 5 0 6 0 0 36 0 6 2 3 1 4 9 1 7 1 4 2 1 16 4 8 2 4 1 4 16 1 9 1 3 0 1 9 0 10 2 3 1 4 9 1 11 1 5 2 1 25 4 12 3 3 1 9 9 1 13 3 4 0 9 16 0 14 1 7 1 1 49 1 15 3 3 3 9 9 9 16 2 5 0 4 25 0 17 2 8 2 4 64 4 18 0 5 3 0 25 9 19 6 1 0 36 1 0 20 4 4 3 16 16 9
Example 2
Following snippet creates a sample data frame −
y1<-round(rnorm(20),2) y2<-round(rnorm(20),2) df2<-data.frame(y1,y2) df2
Output
The following dataframe is created −
y1 y2 1 -0.05 0.98 2 -1.54 -1.98 3 0.66 0.61 4 -0.54 1.22 5 0.34 -0.70 6 -0.26 0.84 7 0.19 -0.20 8 1.39 1.11 9 -1.11 -0.61 10 0.88 -0.10 11 -0.74 -0.03 12 -1.35 -0.94 13 0.99 -1.93 14 1.56 -2.36 15 -0.36 0.95 16 0.38 0.69 17 -0.07 -0.95 18 -0.28 0.68 19 -1.10 0.82 20 0.41 -0.27
To add squared columns in df2, add the following code to the above snippet −
y1<-round(rnorm(20),2) y2<-round(rnorm(20),2) df2<-data.frame(y1,y2) setNames(as.data.frame(cbind(df2,df2^2)), c(names(df2),paste0(names(df2),'_2')))
Output
If you execute all the above given codes as a single program, it generates the following output −
y1 y2 y1_2 y2_2 1 -0.05 0.98 0.0025 0.9604 2 -1.54 -1.98 2.3716 3.9204 3 0.66 0.61 0.4356 0.3721 4 -0.54 1.22 0.2916 1.4884 5 0.34 -0.70 0.1156 0.4900 6 -0.26 0.84 0.0676 0.7056 7 0.19 -0.20 0.0361 0.0400 8 1.39 1.11 1.9321 1.2321 9 -1.11 -0.61 1.2321 0.3721 10 0.88 -0.10 0.7744 0.0100 11 -0.74 -0.03 0.5476 0.0009 12 -1.35 -0.94 1.8225 0.8836 13 0.99 -1.93 0.9801 3.7249 14 1.56 -2.36 2.4336 5.5696 15 -0.36 0.95 0.1296 0.9025 16 0.38 0.69 0.1444 0.4761 17 -0.07 -0.95 0.0049 0.9025 18 -0.28 0.68 0.0784 0.4624 19 -1.10 0.82 1.2100 0.6724 20 0.41 -0.27 0.1681 0.0729
- Related Articles
- How to add a column between columns or after last column in an R data frame?
- How to add name to data frame columns in R?
- How to add one column in an R data frame to rest of the next columns?
- How to add a new column to an R data frame with largest value in each row?
- How to find the square root of each value in columns if some columns are categorical in R data frame?
- How to add a prefix to columns of an R data frame?
- How to add a column in an R data frame with consecutive numbers?
- How to add a new column in an R data frame by combining two columns with a special character?
- How to add a string before each numeric value in an R data frame column?
- How to add all columns of an R data frame by row?
- Extract columns with a string in column name of an R data frame.
- How to drop data frame columns in R by using column name?
- How to add a rank column in base R of a data frame?
- How to add a new column in an R data frame with count based on factor column?
- How to convert multiple columns into single column in an R data frame?
