-3

Im pretty new to R and here's (maybe a simple) question:

I have big .dat datasets and I add together two of them to get the sum of the values. The datasets kinda look like this:

#stud1     
AMR  X1  X2  X3...
  1   3   4  10
  2   4   5   2

#stud2 
AMR  X1  X2  X3
  1   6   4   6
  2   1   2   1

So what I did is

> studAll <- stud1 + stud2

and the result was:

# studAll:
AMR  X1  X2   X3
  2   9   8   16
  4   5   7    3

MY PROBLEM NOW IS: The AMR column is not meant to change, so my idea was to divide this column through the value "2" to get to the former values. Or is there another solution easier than my idea?

rmuc8
  • 2,869
  • 7
  • 27
  • 36
dave_d
  • 1
  • 1

1 Answers1

1

If I understand your question correctly you want to make a new dataframe which adds all the columns except AMR?

You could do it the long way:

studAll$X1 <- Stud1$X1 + Stud2$X1
repeat for each X...

Or this would work if the AMR column is preserved accross all

#set up
stud1 =data.frame(c(1, 2), c(3,4),c(4,5),c(10,2))
stud2 <- stud1
cols <- (c("AMR", "X1", "X2", "X3"))
colnames(stud1) <- cols
colnames(stud2) <- cols

#add them
studAll = stud1 + stud2   
#replace the AMR column into studAll from stud1
#this assumes the AMR column is the same in all studs'
studAll$X1 <- stud1$X1

You could also select all columns other than AMR and add them See for example here http://www.r-tutor.com/r-introduction/data-frame

docjg
  • 99
  • 1
  • 2