0

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!

2 Answers 2

2
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
Sign up to request clarification or add additional context in comments.

3 Comments

Or more simply data$newcol <- ( data$freq >= 100 ) * 1L
I would rather prefer data$newcol = as.integer(data$freq >= 100)
yes, that too. Point being, it's not necessary to use two calls and you certainly should not use ifelse here when faster vectorised methods exist.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.