简体   繁体   中英

Why does base R `ifelse()` turn my character vectors into matrices intermittently?

library(tidyverse)
df <- tibble(col1 = c(5, 2), col2 = c(6, 4), col3 = c(9, 9))
# # A tibble: 2 x 3
#    col1  col2  col3
#   <dbl> <dbl> <dbl>
# 1     5     6     9
# 2     2     4     9


df.cha <- df %>% mutate(col4 = ifelse(apply(.[, 1:3], 1, sd) > 3,
                                      "True",
                                      "False"))
df.cha$col4
#[1] "False" "True" 

The code above works fine. Column 4 is a column of characters as I'd expect. However, I can add one extra condition to my ifelse statement, that being & .[, 3] > 0 , and all of a sudden R is creating matrices for column 4 instead of leaving it as a character vector like I want. See below.

Why is this?

df.mat <- df %>% mutate(col4 = ifelse(apply(.[, 1:3], 1, sd) > 3 & 
                                        .[, 3] > 0,  # I only added this
                                      "True",
                                      "False"))
df.mat$col4
#      col3   
# [1,] "False"
# [2,] "True" 

apply() converts your input to a matrix first, then when you run sd() across that matrix you get a simple vector.

But when you do .[,3] with a tibble, you get a tibble back. Selecting one column does not simplify to a vector like it does with data.frames. Note that you would get different behavior if df were a data.frame rather than tibble.

So the "problem" isn't with ifelse() really. It's the fact that you are doing comparisons on a tibble so the shape is preserved rather than simplified to a vector. And it just so happens that tibble + greater-than/less-than will return matrix by design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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