Question

i need some advise for the following problem:

I have a dataframe with two columns, one containing the date, the other the frequency of a an event. Now i want to add a third column to this dataframe, wich should contain some binary data: 1 for days with a frequency of 100 and higher, 0 for the lower ones.

Has anyone an idea how to do this in a smart way (i'm affraid of writing it by hand;-)? Thanks for your answer in advance!

Was it helpful?

Solution 2

df$freq.gt.100 = as.integer(df$freq >= 100)

The bit inside brackets evaluates to TRUE or FALSE which can be converted to 1 or 0 via as.integer.

There's nothing to be "afraid" of: you can test the right-hand side of the expression on its own to check it works and only when you are happy with this do you add it as a new column to the original data.

EDIT: didn't see the above answer as I was creating this one and had a call to take!

OTHER TIPS

data$newcol = as.integer(data$freq >= 100)

alternatively

data$newcol = ifelse(data$freq >= 100, 1, 0)

alternatively

data$newcal = 0
data$newcol[data$freq >= 100] = 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top