Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换数据帧R中列表中的空值_R_Dataframe_Data Manipulation_Forecastr - Fatal编程技术网

替换数据帧R中列表中的空值

替换数据帧R中列表中的空值,r,dataframe,data-manipulation,forecastr,R,Dataframe,Data Manipulation,Forecastr,我有一个dataframe,字段为list(),但有时它有空值,我在查询中尝试了replace(is.null(.),0),但什么也没发生 我想: 将列车替换为列表(日期=行中的确切日期,销售=0) 展示火车销售 这是我的数据框: 数据帧内的内容: 谢谢…因为“列车”是一个列表,我们可以循环通过列表,并用0替换NULL元素 library(tidyverse) df1 %>% mutate(train = map(train, ~ replace(.x, is.null(.

我有一个
dataframe
,字段为
list()
,但有时它有空值,我在查询中尝试了
replace(is.null(.),0)
,但什么也没发生

我想:

  • 将列车替换为
    列表(日期=行中的确切日期,销售=0)
  • 展示火车销售
这是我的数据框:

数据帧内的内容:


谢谢…

因为“列车”是一个
列表
,我们可以循环通过
列表
,并用
0
替换
NULL
元素

library(tidyverse)
df1 %>%
    mutate(train = map(train, ~ replace(.x, is.null(.x), 0)))
根据评论,OP希望用“test”中相应的“date”替换
NULL
元素

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

i1由于“列车”是一个
列表
,我们可以在
列表
中循环,并用
0
替换
NULL
元素

library(tidyverse)
df1 %>%
    mutate(train = map(train, ~ replace(.x, is.null(.x), 0)))
根据评论,OP希望用“test”中相应的“date”替换
NULL
元素

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

i1谢谢你,它很管用。。但是我想用列表替换它(日期=行中的确切日期,销售额=0)对不起,我是新来的R@hartono-根据问题,你想用0替换,对吗?@hartono-对不起,我没有听你对替换内容的评论be@hartono-你是说考试的日期吗“如果长度为n,那么会有'n'个日期,而且销售额也应该是长度为n的0吗?@hartono-I更新了帖子。你能检查一下吗谢谢,它能用。。但是我想用列表替换它(日期=行中的确切日期,销售额=0)对不起,我是新来的R@hartono-根据问题,你想用0替换,对吗?@hartono-对不起,我没有听你对替换内容的评论be@hartono-你是说考试的日期吗“如果长度为n,那么会有'n'个日期,而且销售额也应该是长度为n的0吗?@hartono-I更新了帖子。你能查一下吗