
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Column of Square Root in Data Frames Stored in R List
To create a column of square root in data frames stored in R list, we can follow the below steps −
First of all, create a list of data frames.
Then, use lapply function to create a column of square root in data frames stored in the list.
Example
Create the list of data frames
Using data.frame function to create data frames and list function to create the list of those data frames −
df1<-data.frame(x=rpois(25,5)) df2<-data.frame(x=rpois(25,2)) List<-list(df1,df2) List
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[[1]] x 1 6 2 3 3 3 4 8 5 7 6 3 7 4 8 6 9 5 10 6 11 1 12 1 13 8 14 4 15 5 16 7 17 5 18 4 19 4 20 6 21 8 22 3 23 3 24 7 25 8 [[2]] x 1 3 2 0 3 0 4 3 5 1 6 4 7 3 8 2 9 3 10 0 11 1 12 2 13 2 14 2 15 2 16 2 17 1 18 2 19 1 20 1 21 4 22 0 23 3 24 0 25 1
Create a column of square root in data frames stored in the list
Using lapply function to create a column of square root in data frames df1 and df2 stored in the list called List as shown below −
df1<-data.frame(x=rpois(25,5)) df2<-data.frame(x=rpois(25,2)) List<-list(df1,df2) lapply(List,function(x) { + x$SquareRoot<-sqrt(x$x) + return(x) + })
Output
[[1]] x SquareRoot 1 6 2.449490 2 3 1.732051 3 3 1.732051 4 8 2.828427 5 7 2.645751 6 3 1.732051 7 4 2.000000 8 6 2.449490 9 5 2.236068 10 6 2.449490 11 1 1.000000 12 1 1.000000 13 8 2.828427 14 4 2.000000 15 5 2.236068 16 7 2.645751 17 5 2.236068 18 4 2.000000 19 4 2.000000 20 6 2.449490 21 8 2.828427 22 3 1.732051 23 3 1.732051 24 7 2.645751 25 8 2.828427 [[2]] x SquareRoot 1 3 1.732051 2 0 0.000000 3 0 0.000000 4 3 1.732051 5 1 1.000000 6 4 2.000000 7 3 1.732051 8 2 1.414214 9 3 1.732051 10 0 0.000000 11 1 1.000000 12 2 1.414214 13 2 1.414214 14 2 1.414214 15 2 1.414214 16 2 1.414214 17 1 1.000000 18 2 1.414214 19 1 1.000000 20 1 1.000000 21 4 2.000000 22 0 0.000000 23 3 1.732051 24 0 0.000000 25 1 1.000000
Advertisements