
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
Sum of Squared Values of an R Data Frame Column
To find the sum of squared values of an R data frame column, we can simply square the column with ^ sign and take the sum using sum function. For example, if we have a data frame called df that contains a column say V then the sum of squared values of V can be found by using the command sum(df$V^2).
Example
Consider the below data frame −
ID<-1:20 x<-rpois(20,5) df1<-data.frame(ID,x) df1
Output
ID x 1 1 3 2 2 9 3 3 7 4 4 4 5 5 6 6 6 4 7 7 7 8 8 4 9 9 8 10 10 5 11 11 6 12 12 7 13 13 7 14 14 7 15 15 5 16 16 4 17 17 4 18 18 5 19 19 4 20 20 5
Finding the sum of squared values in column x of df1 −
Example
sum(df1$x^2)
Output
[1] 667
Example
S.No<-LETTERS[1:20] y<-rnorm(20,5,1) df2<-data.frame(S.No,y) df2
Output
S.No y 1 A 4.398238 2 B 5.543076 3 C 3.089420 4 D 7.313162 5 E 6.389394 6 F 5.718104 7 G 4.999203 8 H 5.835729 9 I 5.078716 10 J 3.507107 11 K 5.712762 12 L 2.778876 13 M 4.379454 14 N 5.487530 15 O 6.192156 16 P 5.065865 17 Q 4.984204 18 R 4.925256 19 S 4.522911 20 T 4.957369
Finding the sum of squared values in column y of df2 −
Example
sum(df2$y^2)
Output
[1] 531.5479
Advertisements