test$G5 <- ifelse((test$col1 > 5 & test$col2 > 5) %in% TRUE,"Yes","No")
test$G5 <- ifelse(((test$col1 > 5) %in% TRUE | is.na(test$col1)) & ((test$col2 > 5) %in% TRUE | is.na(test$col2)),"No")
有关更多信息,请参见this post
,
对我来说,我认为最有益的方法是使用dplyr
的{{1}}函数并明确说明应如何处理您提到的case_when
案例。
复制您的示例(请注意,我在这里明确设置了NA。NA是R无法处理数字矢量中的字符串(“ NA”)的结果。
NA
对于col1 = as.numeric(c(10,2,15,NA_real_,15))
col2 = as.numeric(c(15,NA_real_))
test <- data.frame(col1,col2)
函数和mutate
函数,我正在加载case_when
。如果您不熟悉dplyr
,就好比带有多个条件的ifelse。每个条件后面都有一个“〜”波浪号。如果满足条件,则代字号后面会分配什么。要将“其他所有内容”设置为某个值X,请键入case_when
,因为显然对于先前条件中未满足的所有其他情况,该值都被评估为true。
这应该做您想要的:
TRUE ~ "x"
,
这是一种没有ifelse
或任何其他决策指令(switch
或case_when
)的方法。
i <- with(test,(col1 > 5 & col2 > 5) | is.na(col1) | is.na(col2))
test$G5 <- c("No","Yes")[i + 1]
test
# col1 col2 G5
#1 10 15 Yes
#2 2 15 No
#3 15 2 No
#4 2 2 No
#5 NA 15 Yes
#6 15 NA Yes
,
这是另一个使用$client = new Client([
'base_uri' => 'https://my.endpoint.com/api','headers' => [
'Accept' => 'application/json',...other headers...
]
]);
$data = [...your big slab of data...];
$response = $client->post('/kitely/path',['json' => $data]);
// a string containing the results,which will depend on the endpoint
// the Accept header says we will accept json if it is available
// then we can use json_decode on the result
$result = $response->getBody()->getContents();
的人
rowSums
另一种方法是将所有test$G5 <- c("No","Yes")[(rowSums(is.na(test) | test > 5) == ncol(test)) + 1]
test
# col1 col2 G5
#1 10 15 Yes
#2 2 15 No
#3 15 2 No
#4 2 2 No
#5 NA 15 Yes
#6 15 NA Yes
替换为NA
,然后与5进行比较。
Inf
本文链接:https://www.f2er.com/3048151.html