R-创建一列字符矩阵

R-创建一列字符矩阵,r,dplyr,lapply,R,Dplyr,Lapply,这是我的可复制数据框: library(tidyverse) df <- structure(list(PN = c("41681", "16588", "34881", "36917", "33116", "68447"), `2017-10` = c(0L, 0L, 0L, 0L, 0L, 0L), `2017-11` = c(0L, 1L, 0L, 0L, 0L, 0L), `2017-12` = c(0L, 0L, 0L, 0L, 1L, 0L), `2018-01` = c

这是我的可复制数据框:

library(tidyverse)
df <- structure(list(PN = c("41681", "16588", "34881", 
"36917", "33116", "68447"), `2017-10` = c(0L, 
0L, 0L, 0L, 0L, 0L), `2017-11` = c(0L, 1L, 0L, 0L, 0L, 0L), `2017-12` = c(0L, 
0L, 0L, 0L, 1L, 0L), `2018-01` = c(0L, 0L, 1L, 1L, 0L, 0L), `2018-02` = c(1L, 
0L, 0L, 0L, 0L, 0L), `2018-03` = c(0L, 0L, 0L, 0L, 0L, 0L), `2018-04` = c(0L, 
0L, 0L, 0L, 0L, 1L), Status = c("OK", "NOK", "OK", "NOK", "OK", 
"OK")), .Names = c("PN", "2017-10", "2017-11", "2017-12", 
"2018-01", "2018-02", "2018-03", "2018-04", "Status"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
两种堆栈战士@joran和@akrun告诉我,我“创建了一列字符矩阵”,这就是为什么我不断收到“arrange_impl(.data,dots)中的错误:参数1是不受支持的类型矩阵”错误

用简单的英语我做了什么?我是那种还不了解原子矢量和原子粒子之间区别的人。你能简单明了地回答吗


或者你可以告诉我阅读R中关于数据科学或类似内容的第XYZ章。我也会这么认为(可能在评论中)。

要以通常预期的方式行事,
ifelse
需要一个
逻辑的向量作为第一个参数

您在这里输入的内容是(将
替换为
df
):

这不会发生在常规的
数据.frame
上,因为
df[,8]
将转换为向量

df %>% mutate(
  Status = 
    ifelse(
      rowSums(.[, 2:7]) > 0 & .[[8]] > 0, 
      "NOK", 
      "OK"
    )
) %>% str

# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of  9 variables:
# $ PN     : chr  "41681" "16588" "34881" "36917" ...
# $ 2017-10: int  0 0 0 0 0 0
# $ 2017-11: int  0 1 0 0 0 0
# $ 2017-12: int  0 0 0 0 1 0
# $ 2018-01: int  0 0 1 1 0 0
# $ 2018-02: int  1 0 0 0 0 0
# $ 2018-03: int  0 0 0 0 0 0
# $ 2018-04: int  0 0 0 0 0 1
# $ Status : chr  "OK" "OK" "OK" "OK" ...
阅读
?提取
关于
drop
参数,
tibbles
的行为有点像
数据。帧
使用
drop=FALSE
执行

head(iris[,1])
# [1] 5.1 4.9 4.7 4.6 5.0 5.4

head(iris[,1,drop=FALSE])
#   Sepal.Length
# 1          5.1
# 2          4.9
# 3          4.7
# 4          4.6
# 5          5.0
# 6          5.4

head(as_tibble(iris)[,1])
# # A tibble: 6 x 1
#   Sepal.Length
# <dbl>
# 1          5.1
# 2          4.9
# 3          4.7
# 4          4.6
# 5          5.0
# 6          5.4
现在这个结构不再有问题了

另一种方法是使用
if_else
(来自
dplyr
package)代替
ifelse
,它只在解决方案中添加了一个undercore字符,但不会教会我们这么多:)。它在内部进行神奇的转换,就像您在注释中使用
as.vector
所做的那样

获取原始代码并仅添加神奇的
\uuu

df %>% mutate(
  Status = 
    if_else(
      (apply(.[, 2:7], 1, sum) > 0) & 
        (.[, 8] > 0), 
      "NOK", 
      "OK"
    )
) %>% str
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of  9 variables:
# $ PN     : chr  "41681" "16588" "34881" "36917" ...
# $ 2017-10: int  0 0 0 0 0 0
# $ 2017-11: int  0 1 0 0 0 0
# $ 2017-12: int  0 0 0 0 1 0
# $ 2018-01: int  0 0 1 1 0 0
# $ 2018-02: int  1 0 0 0 0 0
# $ 2018-03: int  0 0 0 0 0 0
# $ 2018-04: int  0 0 0 0 0 1
# $ Status : chr  "OK" "OK" "OK" "OK" ...
错误解释

df %>% mutate(
  Status = 
    ifelse(
      (apply(.[, 2:7], 1, sum) > 0) & 
        (.[, 8] > 0), 
      "NOK", 
      "OK"
    )
) %>% str

# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of  9 variables:
# $ PN     : chr  "41681" "16588" "34881" "36917" ...
# $ 2017-10: int  0 0 0 0 0 0
# $ 2017-11: int  0 1 0 0 0 0
# $ 2017-12: int  0 0 0 0 1 0
# $ 2018-01: int  0 0 1 1 0 0
# $ 2018-02: int  1 0 0 0 0 0
# $ 2018-03: int  0 0 0 0 0 0
# $ 2018-04: int  0 0 0 0 0 1
# $ Status : chr [1:6, 1] "OK" "OK" "OK" "OK" ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : NULL
# .. ..$ : chr "2018-04"
显示
Status
是一个6行1列的字符矩阵<代码>排列
不喜欢那样

你为什么得到一个字符矩阵?

  • df[,8]
    是一个TIBLE
  • 因此,
    df[,8]>0
    是一个矩阵
  • 所以
    (apply([2:7],1,sum)>0)和([8]>0)
    是一个矩阵
?ifelse
说明了输出值:

具有相同长度和属性(包括维度和属性)的向量 “类”)作为测试

因此,
Status
将是一个矩阵,一切最终都有意义;)


有关更多信息,请参见
?dplyr::if_else

这应该是
%>%
中的一个问题。假设你在外面做,它是法向量
apply(df[2:7],1,sum)>0[1]TRUE-FALSE
可能是某种错误,我看不到任何字符列matrices@Moody_Mudskipper如果运行
mutate
,然后检查
str
(我使用的是R3.4.4`和
dplyr\u 0.7.5
)它来自
[,8]>0
,因为您有一个TIBLE,而不是
数据帧,所以它仍然是一个TIBLE。改用
[[8]]>0
(test
df[,8]>0&apply(df[,2:7],1,sum)>0
,然后
df[[8]>0&apply(df[,2:7],1,sum)>0
变异(Status=as.vector(Status))
修复了原始问题。问题是我不知道问题是什么。我创建了一个什么/如何/在哪里/为什么/何时创建的“字符矩阵列”?我的原子浮点是什么?地狱般的错误C###?Heeelllllpppppp。谢谢。哇。正是我想要的。如果可以的话,我会再按几次向上箭头。
df %>% mutate(
  Status = 
    if_else(
      (apply(.[, 2:7], 1, sum) > 0) & 
        (.[, 8] > 0), 
      "NOK", 
      "OK"
    )
) %>% str
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of  9 variables:
# $ PN     : chr  "41681" "16588" "34881" "36917" ...
# $ 2017-10: int  0 0 0 0 0 0
# $ 2017-11: int  0 1 0 0 0 0
# $ 2017-12: int  0 0 0 0 1 0
# $ 2018-01: int  0 0 1 1 0 0
# $ 2018-02: int  1 0 0 0 0 0
# $ 2018-03: int  0 0 0 0 0 0
# $ 2018-04: int  0 0 0 0 0 1
# $ Status : chr  "OK" "OK" "OK" "OK" ...
df %>% mutate(
  Status = 
    ifelse(
      (apply(.[, 2:7], 1, sum) > 0) & 
        (.[, 8] > 0), 
      "NOK", 
      "OK"
    )
) %>% str

# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of  9 variables:
# $ PN     : chr  "41681" "16588" "34881" "36917" ...
# $ 2017-10: int  0 0 0 0 0 0
# $ 2017-11: int  0 1 0 0 0 0
# $ 2017-12: int  0 0 0 0 1 0
# $ 2018-01: int  0 0 1 1 0 0
# $ 2018-02: int  1 0 0 0 0 0
# $ 2018-03: int  0 0 0 0 0 0
# $ 2018-04: int  0 0 0 0 0 1
# $ Status : chr [1:6, 1] "OK" "OK" "OK" "OK" ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : NULL
# .. ..$ : chr "2018-04"