1

I have a question for a possible loop. Maybe there is another solution for the problem?

Here the example of my dataframe:

Dataframe example

I want to count the values in column "to count"(always 1) if the values in column "id" are the same and write the result in column "solution?"

After that i can delete column "z" and do "unique"

I think it`s not too hard, but i do not find the right command + I got my Problems with loops :-(

Thanks for help!

Laudl007
  • 51
  • 4
  • 3
    In the future, please don't share your data as screenshots. It's useless that way for testing code. Read the [FAQ](http://stackoverflow.com/q/5963269/1412059) to learn better ways of sharing data. – Roland Aug 03 '13 at 16:01
  • @Roland , Thanks a lot for helping me out. You saved me Weekend ! I will consider the FAQ next time ;-) – Laudl007 Aug 03 '13 at 17:43

2 Answers2

1

You can use ddply from plyr package [Assume mydata is your data)

library(plyr)
ddply(mydata,.(id),transform, solution=length(id))
Jilber Urbina
  • 53,125
  • 10
  • 108
  • 134
Metrics
  • 14,596
  • 7
  • 50
  • 80
1

You can do this all in one step:

library(plyr)

ddply(DF, .(id, x, y), summarise, sumcount=sum(to_count))
Roland
  • 122,144
  • 10
  • 182
  • 276