Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

r - Sum rows by interval Dataframe

I need help in a research project problem.

The code problem is: i have a big data frame called FRAMETRUE, and a need to sum certain columns of those rows by row in a new column that I will call Group1.

For example:

head.table(FRAMETRUE)

Municipalities  1989  1990  1991  1992  1993  1994  1995  1996  1997
A                 3     3     5    2     3      4     2     5     3
B                 7     1     2    4     5      0     4     8     9
C                 10    15    1    3     2      NA    2     5     3
D                 7     0     NA   5     3       6    4     5     5
E                 5     1     2    4     0       3    5     4     2

I must sum the values in the rows from 1989 to 1995 in a new column called Group1. like the column Group1 should be

Group1
  22
  23

and so on...

I know it must be something simple, I just don't get it, I'm still learning R

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are looking for an R solution, here's one way to do it: The trick is using [ combined with rowSums

FRAMETRUE$Group1 <- rowSums(FRAMETRUE[, 2:8], na.rm = TRUE)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...