Laudl007
Laudl007

Reputation: 51

Loop for a dataframe

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!

Upvotes: 1

Views: 128

Answers (2)

Roland
Roland

Reputation: 132576

You can do this all in one step:

library(plyr)

ddply(DF, .(id, x, y), summarise, sumcount=sum(to_count))

Upvotes: 1

Metrics
Metrics

Reputation: 15458

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

library(plyr)
ddply(mydata,.(id),transform, solution=length(id))

Upvotes: 1

Related Questions