calculate row sum and product in data.frame
问题 I would like to append a columns to my data.frame in R that contain row sums and products Consider following data frame x y z 1 2 3 2 3 4 5 1 2 I want to get the following x y z sum prod 1 2 3 6 6 2 3 4 9 24 5 1 2 8 10 I have tried sum = apply(ages,1,add) but it gives me a row vector. Can some one please show me an efficient command to sum and product and append them to original data frame as shown above? 回答1: Try transform(df, sum=rowSums(df), prod=x*y*z) # x y z sum prod #1 1 2 3 6 6 #2 2 3