替换数据帧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更新了帖子。你能查一下吗