R

时间:2015-12-09 15:09:06

标签: r

在.txt中有50000个字符串,每个字符串包含由“”分隔的1024位(值0或1)。我是R的新手,我从自己的50000中获得了一个逻辑列表......(loglist)

.txt的样本(50000中的两个):sry不知道如何格式化...           0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000100000000000000 0000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000 000000000000000000000010

singleString <- paste(readLines("input_7of1024_50k.txt"), collapse=" ")
characterArr <- unlist(strsplit(singleString, split=" ", useBytes="FALSE") )
charList <- strsplit(characterArr, split="", useBytes="FALSE" )
logList <- as.logical(as.numeric(charList[[1]]))

我想要一个如下列表:

[[1]]
    [1] True False False True 
    ....
    [1021] True True False True

[[2]]
    [1] True False False True 
    ....
    [1021] True True False True

我搜索了一个小时的自己,我希望我没有问过两次。很多。

1 个答案:

答案 0 :(得分:1)

首先制作一些样本数据:

cat("1 0 1 0 1 0 1 0 1 0", "0 1 0 1 0 1 0 1 0 1", file = "data.txt",
    sep = "\n")

阅读:

z <- readLines("data.txt")

首先我们对空格进行了分析:

z1 <- lapply(z, function(x) unlist(strsplit(x, " ")))

然后我们转换为数字,然后转换为逻辑:

lapply(z1, function(x) as.logical(as.numeric(x)))
[[1]]
 [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE

[[2]]
 [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE