joh*_*ric 21 r data.table
我使用以下命令从R导出数据:
write.table(output,file="data.raw", na "-9999",sep="\t",row.names=F,col.names=F)
Run Code Online (Sandbox Code Playgroud)
正确导出我的数据,但它将所有逻辑变量导出为TRUE和FALSE.
我需要将数据读入另一个只能处理数值的程序.有没有一种有效的方法在导出期间将这些转换为数字1和0?我有大量的数字变量,所以我希望自动遍历data.table中的所有变量
我意识到我可以在输出数据上运行简单的sed脚本,但看起来这应该是从R直接做的.
或者,我的输出对象是data.table.有没有一种有效的方法将data.table中的所有逻辑变量转换为数字变量?
如果它有用,这里有一些代码来生成一个带有逻辑变量的data.table(它不是大量的逻辑变量,但足以在示例代码上使用):
DT = data.table(cbind(1:100,rnorm(100)>0)
DT[ ,V3:= V2==1 ]
DT[ ,V4:= V2!=1 ]
Run Code Online (Sandbox Code Playgroud)
这似乎是一个简单的问题,但它让我失望,所以谢谢你的帮助!
jos*_*ber 26
对于data.frame,您可以将所有逻辑列转换为数字:
# The data
set.seed(144)
dat <- data.frame(V1=1:100,V2=rnorm(100)>0)
dat$V3 <- dat$V2 == 1
head(dat)
# V1 V2 V3
# 1 1 FALSE FALSE
# 2 2 TRUE TRUE
# 3 3 FALSE FALSE
# 4 4 FALSE FALSE
# 5 5 FALSE FALSE
# 6 6 TRUE TRUE
# Convert all to numeric
cols <- sapply(dat, is.logical)
dat[,cols] <- lapply(dat[,cols], as.numeric)
head(dat)
# V1 V2 V3
# 1 1 0 0
# 2 2 1 1
# 3 3 0 0
# 4 4 0 0
# 5 5 0 0
# 6 6 1 1
Run Code Online (Sandbox Code Playgroud)
在data.table语法中:
# Data
set.seed(144)
DT = data.table(cbind(1:100,rnorm(100)>0))
DT[,V3 := V2 == 1]
DT[,V4 := FALSE]
head(DT)
# V1 V2 V3 V4
# 1: 1 0 FALSE FALSE
# 2: 2 1 TRUE FALSE
# 3: 3 0 FALSE FALSE
# 4: 4 0 FALSE FALSE
# 5: 5 0 FALSE FALSE
# 6: 6 1 TRUE FALSE
# Converting
(to.replace <- names(which(sapply(DT, is.logical))))
# [1] "V3" "V4"
for (var in to.replace) DT[, (var):= as.numeric(get(var))]
head(DT)
# V1 V2 V3 V4
# 1: 1 0 0 0
# 2: 2 1 1 0
# 3: 3 0 0 0
# 4: 4 0 0 0
# 5: 5 0 0 0
# 6: 6 1 1 0
Run Code Online (Sandbox Code Playgroud)
akr*_*run 10
如果有多列,您可以使用set(使用@ josilber的示例)
library(data.table)
Cols <- which(sapply(dat, is.logical))
setDT(dat)
for(j in Cols){
set(dat, i=NULL, j=j, value= as.numeric(dat[[j]]))
}
Run Code Online (Sandbox Code Playgroud)
那只是一个:
dat <- data.frame(le = letters[1:10], lo = rep(c(TRUE, FALSE), 5))
dat
le lo
1 a TRUE
2 b FALSE
3 c TRUE
4 d FALSE
5 e TRUE
6 f FALSE
7 g TRUE
8 h FALSE
9 i TRUE
10 j FALSE
dat$lo <- as.numeric(dat$lo)
dat
le lo
1 a 1
2 b 0
3 c 1
4 d 0
5 e 1
6 f 0
7 g 1
8 h 0
9 i 1
10 j 0
Run Code Online (Sandbox Code Playgroud)
dplyr如果案件(没有人知道)你的数据将被导入R,或者另一种方法可以保留前一列.
library(dplyr)
dat <- dat %>% mutate(lon = as.numeric(lo))
dat
Source: local data frame [10 x 3]
le lo lon
1 a TRUE 1
2 b FALSE 0
3 c TRUE 1
4 d FALSE 0
5 e TRUE 1
6 f FALSE 0
7 g TRUE 1
8 h FALSE 0
9 i TRUE 1
10 j FALSE 0
Run Code Online (Sandbox Code Playgroud)
我不知道我的代码是否正在执行,但是它会检查所有列并仅将那些逻辑更改为数字.当然,如果你的TRUE和FALSE不是逻辑但字符串(可能是远程的),我的代码将无法正常工作.
for(i in 1:ncol(dat)){
if(is.logical(dat[, i]) == TRUE) dat[, i] <- as.numeric(dat[, i])
}
Run Code Online (Sandbox Code Playgroud)
As Ted Harding pointed out in the R-help mailing list, one easy way to convert logical objects to numeric is to perform an arithmetic operation on them. Convenient ones would be * 1 and + 0, which will keep the TRUE/FALSE == 1/0 paradigm.
For your mock data (I've changed the code a bit to use regular R packages and to reduce size):
df <- data.frame(cbind(1:10, rnorm(10) > 0))
df$X3 <- df$X2 == 1
df$X4 <- df$X2 != 1
Run Code Online (Sandbox Code Playgroud)
The dataset you get has a mixture of numeric and boolean variables:
X1 X2 X3 X4
1 1 0 FALSE TRUE
2 2 0 FALSE TRUE
3 3 1 TRUE FALSE
4 4 1 TRUE FALSE
5 5 1 TRUE FALSE
6 6 0 FALSE TRUE
7 7 0 FALSE TRUE
8 8 1 TRUE FALSE
9 9 0 FALSE TRUE
10 10 1 TRUE FALSE
Run Code Online (Sandbox Code Playgroud)
Now let
df2 <- 1 * df
Run Code Online (Sandbox Code Playgroud)
(If your dataset contains character or factor variables, you will need to apply this operation to a subset of df filtering out those variables)
df2 is equal to
X1 X2 X3 X4
1 1 0 0 1
2 2 0 0 1
3 3 1 1 0
4 4 1 1 0
5 5 1 1 0
6 6 0 0 1
7 7 0 0 1
8 8 1 1 0
9 9 0 0 1
10 10 1 1 0
Run Code Online (Sandbox Code Playgroud)
Which is 100% numeric, as str(df2) will show you.
Now you can safely export df2 to your other program.
小智 6
最简单的方法!
将矩阵乘以1
例如:
A <- matrix(c(TRUE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,TRUE),ncol=4)
A
Run Code Online (Sandbox Code Playgroud)
#[,1] [,2] [,3] [,4]
#[1,] TRUE TRUE TRUE FUESE
#[2,] FALSE TRUE FALSE TRUE
B <- 1*A
B
Run Code Online (Sandbox Code Playgroud)
#[,1] [,2] [,3] [,4]
#[1,] 1 1 1 0
#[2,] 0 1 0 1
(你也可以加零:B <- 0+A)
小智 6
一条线解决方案
使用以下代码,我们获取所有逻辑列并将它们设为数字。
library(magrittr)
dat %<>% mutate_if(is.logical,as.numeric)
Run Code Online (Sandbox Code Playgroud)