对数据框中的列执行二进制函数

发布于 2024-08-21 12:41:06 字数 614 浏览 3 评论 0原文

假设我有一个包含以下内容的数据框:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末 2024-08-28 12:41:06

如果您想明确分配(并对 3 的截止值进行硬编码),您可以使用

d$Day <- ifelse(d$trial<3, 1, 2)

This is a bit more transparent.否则,正如您所发现的,进行算术运算会将逻辑值转换为数字。您可以使用 as.numericas.integer 自行完成:

as.integer(FALSE)  #0
as.integer(TRUE)   #1

If you want to be explicit with the assignment (and hard-coding the cutoff of 3), you can use

d$Day <- ifelse(d$trial<3, 1, 2)

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 or as.integer:

as.integer(FALSE)  #0
as.integer(TRUE)   #1
懒的傷心 2024-08-28 12:41:06

获取数据:

x <- read.table(textConnection(
"Trial Person 
1     John   
2     John   
3     John   
4     John
1     Bill 
2     Bill
3     Bill
4     Bill"), header=TRUE)

我认为您当前的方法是正确的(注意:您不需要 as.numeric,因为在这种情况下进行加法时它会自动转换):

(x$Trial >= 3) + 1

否则,这是一种使用 plyr 执行此操作的方法。

library(plyr)
ddply(x, .(Person), transform, Day=rep(c(1,2), each=2))

Get the data:

x <- read.table(textConnection(
"Trial Person 
1     John   
2     John   
3     John   
4     John
1     Bill 
2     Bill
3     Bill
4     Bill"), header=TRUE)

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):

(x$Trial >= 3) + 1

Otherwise, here's a way to do it with plyr.

library(plyr)
ddply(x, .(Person), transform, Day=rep(c(1,2), each=2))
青衫儰鉨ミ守葔 2024-08-28 12:41:06

更一般地说,如果您尝试将 c(1,2,3,4,5,6) 形式的向量转换为 c(1,1,2,2, 3,3),就好像您每天有两次试验,那么您可能需要使用整数除法来表达:

> x <- 1:6
> x
[1] 1 2 3 4 5 6
> (x-1) %/% 2 + 1
[1] 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) to c(1,1,2,2,3,3), as if you had two trials per day, then you might want to express this using integer division:

> x <- 1:6
> x
[1] 1 2 3 4 5 6
> (x-1) %/% 2 + 1
[1] 1 1 2 2 3 3
寄居人 2024-08-28 12:41:06

好的,所以我找到了一个解决方案,如果我这样做,

(d$trial>=3)+1

它将布尔值转换为整数并且它可以工作......但是,有没有更好的方法来做到这一点?

Ok, so I found a solution, if I do

(d$trial>=3)+1

It converts the boolean to an integer and it works ... however, is there a better way to do this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文