Applying the same function to every row of a data.frame - R

0 votes

I am working with the 'iris' data-set:

       Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2      setosa
2          4.9         3.0          1.4         0.2      setosa
3          4.7         3.2          1.3         0.2      setosa
4          4.6         3.1          1.5         0.2      setosa
5          5.0         3.6          1.4         0.2      setosa
6          5.4         3.9          1.7         0.4      setosa

I want to apply the sum function for the for the first 4 columns of this data-set for each row and get a vector of size 150 as the result. i.e I would want to get a 150-size vector containing the sum of 

'Sepal.Length + Sepal.Width + Petal.Length + Petal.Width' for every row

May 22, 2018 in Data Analytics by DeepCoder786
• 1,700 points
24 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use the 'appply()' function for this purpose:

apply(iris[1:4], 1, sum)

The apply() function takes in 3 parameters over here:

1st parameter: dataset/matrix
2nd parameter: function to be applied row-wise/column-wise

3rd parameter: function to be applied

So, this would be the desired result of the command:

apply(iris[1:4], 1, sum)
  [1] 10.2  9.5  9.4  9.4 10.2 11.4  9.7 10.1  8.9  9.6 10.8 10.0  9.3  8.5 11.2 12.0 11.0 10.3
 [19] 11.5 10.7 10.7 10.7  9.4 10.6 10.3  9.8 10.4 10.4 10.2  9.7  9.7 10.7 10.9 11.3  9.7  9.6
 [37] 10.5 10.0  8.9 10.2 10.1  8.4  9.1 10.7 11.2  9.5 10.7  9.4 10.7  9.9 16.3 15.6 16.4 13.1
 [55] 15.4 14.3 15.9 11.6 15.4 13.2 11.5 14.6 13.2 15.1 13.4 15.6 14.6 13.6 14.4 13.1 15.7 14.2
 [73] 15.2 14.8 14.9 15.4 15.8 16.4 14.9 12.8 12.8 12.6 13.6 15.4 14.4 15.5 16.0 14.3 14.0 13.3
 [91] 13.7 15.1 13.6 11.6 13.8 14.1 14.1 14.7 11.7 13.9 18.1 15.5 18.1 16.6 17.5 19.3 13.6 18.3
[109] 16.8 19.4 16.8 16.3 17.4 15.2 16.1 17.2 16.8 20.4 19.5 14.7 18.1 15.3 19.2 15.7 17.8 18.2
[127] 15.6 15.8 16.9 17.6 18.2 20.1 17.0 15.7 15.7 19.1 17.7 16.8 15.6 17.5 17.8 17.4 15.5 18.2
[145] 18.2 17.2 15.7 16.7 17.3 15.8
answered May 22, 2018 by Bharani
• 4,550 points

Related Questions In Data Analytics

0 votes
1 answer

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$symb ...READ MORE

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
47 views
0 votes
1 answer
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
53 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
23 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
32 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 35,800 points
23 views
0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

Custom Function to replace missing values in a vector with the mean of values

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
31 views
0 votes
1 answer

Locating row index of a column which has the maximum value - R

which.max(iris$Sepal.Length) This command will give you the desired ...READ MORE

answered May 16, 2018 in Data Analytics by Bharani
• 4,550 points
45 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.