R:从数据帧创建二进制数据 [英] R: Create binary data from a data frame

查看:75
本文介绍了R:从数据帧创建二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下问题,我需要一些建议:

i need some advise for the following problem:

我有一个包含两列的数据框,一列包含日期,另一列包含事件的发生频率. 现在,我想在此数据框中添加第三列,该列应包含一些二进制数据:1表示频率为100或更高的天,0表示较低的频率.

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!

推荐答案

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

括号内的位的值为TRUEFALSE,可以通过as.integer转换为1或0.

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.

当我创建此答案并打电话接听时,没有看到以上答案!

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

这篇关于R:从数据帧创建二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆