我有一个dataframewith 字段list(),但有时它有空值,我replace(is.null(.), 0)在查询中尝试过,但什么也没发生。
我想要 :
- 将火车替换为
list(date=exactly date in the rows, sales=0) - 显示 train.sales
这是我的数据框: https ://pasteboard.co/IsclvYT.png
数据框内的内容: https ://pasteboard.co/IscmiwV.png
谢谢...
我有一个dataframewith 字段list(),但有时它有空值,我replace(is.null(.), 0)在查询中尝试过,但什么也没发生。
我想要 :
list(date=exactly date in the rows, sales=0)这是我的数据框: https ://pasteboard.co/IsclvYT.png
数据框内的内容: https ://pasteboard.co/IscmiwV.png
谢谢...
由于 'train' 是 a list,我们可以遍历 thelist并将NULL元素替换为0
library(tidyverse)
df1 %>%
mutate(train = map(train, ~ replace(.x, is.null(.x), 0)))
根据评论,OP希望用NULL“test”中的相应“日期”替换元素
df1 %>%
mutate(train = map2(train, test, ~ if(is.null(.x)) list(date = .y$date, sales = rep(0, length(.y$sales))) else .x))
或使用base R
i1 <- sapply(df1$train, is.null)
df1$train[i1] <- 0