对数据框中的列执行二进制函数
假设我有一个包含以下内容的数据框:
Trial Person
1 John
2 John
3 John
4 John
1 Bill
2 Bill
3 Bill
4 Bill
并且我想将其转换为
Trial Person Day
1 John 1
2 John 1
3 John 2
4 John 2
1 Bill 1
2 Bill 1
3 Bill 2
4 Bill 2
轻松实现它
Trial Person Day
1 John TRUE
2 John TRUE
3 John FALSE
4 John FALSE
1 Bill TRUE
2 Bill TRUE
3 Bill FALSE
4 Bill FALSE
我可以通过执行 d$day=d$Trial<3
,但是我怎样才能得到我想要的东西?
Say I have a data frame with the contents:
Trial Person
1 John
2 John
3 John
4 John
1 Bill
2 Bill
3 Bill
4 Bill
and I want to transform this to
Trial Person Day
1 John 1
2 John 1
3 John 2
4 John 2
1 Bill 1
2 Bill 1
3 Bill 2
4 Bill 2
I can very easily make it
Trial Person Day
1 John TRUE
2 John TRUE
3 John FALSE
4 John FALSE
1 Bill TRUE
2 Bill TRUE
3 Bill FALSE
4 Bill FALSE
by doing d$day=d$trial<3
but how can I get to what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想明确分配(并对 3 的截止值进行硬编码),您可以使用
This is a bit more transparent.否则,正如您所发现的,进行算术运算会将逻辑值转换为数字。您可以使用
as.numeric
或as.integer
自行完成:If you want to be explicit with the assignment (and hard-coding the cutoff of 3), you can use
This is a bit more transparent. Otherwise, as you discovered, doing an arithmetic operation will convert the logical value to numeric. You can do it yourself by using
as.numeric
oras.integer
:获取数据:
我认为您当前的方法是正确的(注意:您不需要 as.numeric,因为在这种情况下进行加法时它会自动转换):
否则,这是一种使用 plyr 执行此操作的方法。
Get the data:
I think that your current approach is the right one (note: you don't need as.numeric, because it's automatically cast when doing addition in this case):
Otherwise, here's a way to do it with plyr.
更一般地说,如果您尝试将
c(1,2,3,4,5,6)
形式的向量转换为c(1,1,2,2, 3,3)
,就好像您每天有两次试验,那么您可能需要使用整数除法来表达:More generally, if you're trying to convert a vector of the form
c(1,2,3,4,5,6)
toc(1,1,2,2,3,3)
, as if you had two trials per day, then you might want to express this using integer division:好的,所以我找到了一个解决方案,如果我这样做,
它将布尔值转换为整数并且它可以工作......但是,有没有更好的方法来做到这一点?
Ok, so I found a solution, if I do
It converts the boolean to an integer and it works ... however, is there a better way to do this?