【问题标题】:longer object length is not a multiple of shorter object length using MAP2 function较长的对象长度不是使用 MAP2 函数的较短对象长度的倍数
【发布时间】:2020-11-30 23:13:31
【问题描述】:

我正在尝试使用以下代码为每个主题和 lbtestcd 生成图。 基本上我正在尝试使用map2 函数从数据框中迭代两个列表,而我的匿名函数是ggplot 来生成绘图。运行我的代码时,我收到以下警告消息。有人能帮帮我吗?

警告信息: 1:在 USUBJID == .x 中: 较长的对象长度不是较短对象长度的倍数 2:在 LBTESTCD == .y 中: 较长的对象长度不是较短对象长度的倍数

library(tidyverse)
library(sqldf)

# Get List of subjects and testcds and convert to dataframe 

sub_list  <- data.frame(subs = unique(adhoc_lb$USUBJID))
test_list <- data.frame(testcd = unique(adhoc_lb$LBTESTCD))


# create a data frame with subjects and testcds
mer <- sqldf("select a.*,b.*
              from sub_list as a , test_list as b")

# Each Graph in a separate page
allplots <- map2(.x=mer[1], .y=mer[2] ,.f= ~ (adhoc_lb %>% 
            filter(USUBJID == .x | LBTESTCD == .y) %>%     
            ggplot(aes(x = VISITNUM, y = AVAL)) +
            geom_point()))

【问题讨论】:

  • 警告信息非常明确。 mer[1]mer[2] 的长度不同,也不是彼此长度的整数倍。你检查过每个的长度吗?
  • 这就是我检查长度的方式,它们的长度相同 > length(mer[1]) [1] 1 > length(mer[2]) [1] 1跨度>
  • 嗯...您是否目视检查了mer[1]mer[2] 以查看它们的外观?
  • allplots % filter(USUBJID == .x | LBTESTCD == .y) %>% ggplot(aes(x = VISITNUM, y = AVAL)) + geom_point())) 将 mer[1] ,mer[2] 更改为 mer$subs 和 mer$testcd 似乎有效,但又不确定为什么以前的努力。

标签: r dataframe loops ggplot2


【解决方案1】:

mer[1] 将返回一个数据帧。使用 mer[[1]]mer[, 1] 会返回一个类似于使用 $ 访问数据框列值时的向量。

library(tidyverse)

allplots <- map2(.x=mer[[1]], .y=mer[[2]] ,
                 .f= ~ adhoc_lb %>% 
                        filter(USUBJID == .x | LBTESTCD == .y) %>%     
                        ggplot(aes(x = VISITNUM, y = AVAL)) + geom_point())

【讨论】:

  • mer[[1]] 或 mer$subs 都可以。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
相关资源
最近更新 更多