简体   繁体   English

使用逻辑从现有的多列数据帧生成新的多列数据帧

[英]Produce new multi-column data frame from existing multi-column data frames with logic

I have 3 data frames each with multiple columns.我有 3 个数据框,每个数据框都有多列。 All columns are numeric values, and I would like to produce a fourth with logic like所有列都是数值,我想用逻辑生成第四个

# this doesn't work when data frames have more than one column
d <- ifelse(A > 0, B, C)

However all the samples of ifelse I've seen produce only a single output column.然而,我见过的所有ifelse样本只产生一个输出列。 I'd like to operate on all columns.我想对所有列进行操作。 How can I achieve this?我怎样才能做到这一点?

For example, given these data frames例如,给定这些数据帧

pivot <- data.frame(a=c(-1, 1), b=c(1, 1))
low <- data.frame(a=c(2.2, 3.3), b=c(4.4, 5.5))
high  <- data.frame(a=c(10.1, 11.2), b=c(12.3, 14.4))

I'd like some expression like我想要一些表达

result <- ifelse(pivot > 0, high, low)

to produce生产

data.frame(a=c(2.2, 11.2), b=c(12.3, 14.4))

Converting to matrix will solve the case转换为matrix将解决此问题

 as.data.frame(ifelse(pivot > 0, as.matrix(high), as.matrix(low)))

-output -输出

    a    b
1  2.2 12.3
2 11.2 14.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM