R数据表用0替换NULL从不更改值

R数据表用0替换NULL从不更改值,r,data.table,R,Data.table,这看起来很简单,但似乎根本不起作用。。我的要求只是将空值替换为0。我没有尝试lappy和其他多行解决方案,因为我觉得有一个简单的解决方案。还是我错了 下面是根据配置单元查询的输出创建的数据表。输出是以制表符分隔的字符串(NULL\tNULL)。所以我猜空值是一个字符串。使用下面的代码将其转换为数据表 df <- read.table(text=ret_text, sep = "\t") #data frame dt=as.data.table(df) dput输出 > d

这看起来很简单,但似乎根本不起作用。。我的要求只是将空值替换为0。我没有尝试lappy和其他多行解决方案,因为我觉得有一个简单的解决方案。还是我错了

下面是根据配置单元查询的输出创建的数据表。输出是以制表符分隔的字符串(
NULL\tNULL
)。所以我猜空值是一个字符串。使用下面的代码将其转换为数据表

  df <- read.table(text=ret_text, sep = "\t") #data frame
  dt=as.data.table(df) 
dput
输出

> dput(ret_data)
structure(list(V1 = structure(1L, .Label = "NULL", class = "factor"), 
    V2 = structure(1L, .Label = "NULL", class = "factor")), .Names = c("V1", 
"V2"), class = c("data.table", "data.frame"), row.names = c(NA, 
-1L), .internal.selfref = <pointer: 0x3527fd8>)

除非数据的结构被
dput
神奇地歪曲了,否则这应该对您有用:

# load data table library
library(data.table)
# construct the sample dataset
ret_data <- structure(list(V1 = structure(1L, .Label = "NULL", class = "factor"), 
               V2 = structure(1L, .Label = "NULL", class = "factor")),
               .Names = c("V1", "V2"),
               class = c("data.table", "data.frame"), row.names = c(NA, -1L))

# choose columns to be altered
# you might also want to change just some of the columns
cols <- colnames(ret_data)
# for each of the columns, perform an operation
for (j in cols) {set(ret_data, j = j, value = ifelse(as.character(ret_data[[j]]) == "NULL", 0, 1))}
#加载数据表库
库(数据表)
#构建示例数据集

ret_data除非数据的结构被
dput
神奇地歪曲了,否则这应该对您有用:

# load data table library
library(data.table)
# construct the sample dataset
ret_data <- structure(list(V1 = structure(1L, .Label = "NULL", class = "factor"), 
               V2 = structure(1L, .Label = "NULL", class = "factor")),
               .Names = c("V1", "V2"),
               class = c("data.table", "data.frame"), row.names = c(NA, -1L))

# choose columns to be altered
# you might also want to change just some of the columns
cols <- colnames(ret_data)
# for each of the columns, perform an operation
for (j in cols) {set(ret_data, j = j, value = ifelse(as.character(ret_data[[j]]) == "NULL", 0, 1))}
#加载数据表库
库(数据表)
#构建示例数据集

ret_data当前
NULL
不是
NULL
,而是一个
因子。将其转换为字符并按您的意愿继续

 df<-df %>% 
  mutate_if(is.factor,as.character) %>% 
  str_replace_all("NULL","0")
df%
如果(is.factor,as.character)%>%,则进行变异
str_replace_all(“空”、“0”)

如果您愿意,可以将其设置为数值。

当前的
NULL
不是
NULL
,而是一个
因子。将其转换为字符并按您的意愿继续

 df<-df %>% 
  mutate_if(is.factor,as.character) %>% 
  str_replace_all("NULL","0")
df%
如果(is.factor,as.character)%>%,则进行变异
str_replace_all(“空”、“0”)

如果您愿意,您可以将其设置为数值。

您可以发布dt或ret_数据的
dput
吗?@docendodiscimus编辑此帖子以添加
dput
您的问题可能是
ret_数据
从不为
NULL
,它是一个数据表,有两列,其中包含
NULL
values@ira是,数据表在2个单元格中包含2个空值。如果将列转换为字符,然后与
“NULL”
进行比较,会发生什么情况?e、 g.
as.character(ret_data[,V1])==“NULL”
?你能发布dt或ret_数据的
dput
吗?@docendodiscimus编辑了这篇文章以添加
dput
你的问题可能是
ret_data
从来都不是
NULL
,它是一个数据表,有两列,其中包含
NULL
values@ira是,数据表在2个单元格中包含2个空值。如果将列转换为字符,然后与
“NULL”
进行比较,会发生什么情况?e、 g.
as.字符(ret_data[,V1])==“NULL”
# load data table library
library(data.table)
# construct the sample dataset
ret_data <- structure(list(V1 = structure(1L, .Label = "NULL", class = "factor"), 
               V2 = structure(1L, .Label = "NULL", class = "factor")),
               .Names = c("V1", "V2"),
               class = c("data.table", "data.frame"), row.names = c(NA, -1L))

# choose columns to be altered
# you might also want to change just some of the columns
cols <- colnames(ret_data)
# for each of the columns, perform an operation
for (j in cols) {set(ret_data, j = j, value = ifelse(as.character(ret_data[[j]]) == "NULL", 0, 1))}
 df<-df %>% 
  mutate_if(is.factor,as.character) %>% 
  str_replace_all("NULL","0")