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
How to create a column of raise to the power 3 in data frames stored in R list?
To create a column of raise to the power 3 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 raise to the power 3 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=sample(1:50,25)) df2<-data.frame(x=sample(1:50,25)) 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 40 2 45 3 29 4 33 5 17 6 10 7 44 8 23 9 6 10 28 11 18 12 27 13 21 14 25 15 5 16 1 17 35 18 50 19 34 20 41 21 16 22 22 23 24 24 8 25 39 [[2]] x 1 45 2 6 3 41 4 7 5 12 6 13 7 11 8 3 9 43 10 36 11 27 12 30 13 8 14 25 15 22 16 31 17 48 18 4 19 47 20 35 21 26 22 46 23 37 24 42 25 32
Create a column of raise to the power 3 in data frames stored in the list
Using lapply function to create a column of raise to the power 3 in data frames df1 and df2 stored in the list called List as shown below −
df1<-data.frame(x=sample(1:50,25))
df2<-data.frame(x=sample(1:50,25))
List<-list(df1,df2)
lapply(List,function(x) {
+ x$Power3<-x$x^3
+ return(x)
+ })
Output
[[1]]
x Power3
1 40 64000
2 45 91125
3 29 24389
4 33 35937
5 17 4913
6 10 1000
7 44 85184
8 23 12167
9 6 216
10 28 21952
11 18 5832
12 27 19683
13 21 9261
14 25 15625
15 5 125
16 1 1
17 35 42875
18 50 125000
19 34 39304
20 41 68921
21 16 4096
22 22 10648
23 24 13824
24 8 512
25 39 59319
[[2]]
x Power3
1 45 91125
2 6 216
3 41 68921
4 7 343
5 12 1728
6 13 2197
7 11 1331
8 3 27
9 43 79507
10 36 46656
11 27 19683
12 30 27000
13 8 512
14 25 15625
15 22 10648
16 31 29791
17 48 110592
18 4 64
19 47 103823
20 35 42875
21 26 17576
22 46 97336
23 37 50653
24 42 74088
25 32 32768Advertisements