Matthew Keller
2010-Apr-25 16:52 UTC
[R] how to make read in a vector of 0s and 1s with no space between them
Hi all, Probably a rudimentary question. I have a flat file that looks like this (the real one has ~10e6 elements): 10110100101001011101011 and I want to pull that into R as a vector, but with each digit being it's own element. There are no separators between the digits. How can I accomplish this? Thanks in advance! Matt -- Matthew C Keller Asst. Professor of Psychology University of Colorado at Boulder www.matthewckeller.com
Tal Galili
2010-Apr-25 17:00 UTC
[R] how to make read in a vector of 0s and 1s with no space between them
*Solution:* strsplit(c("10110100101001011101011"), "") But make sure you change the output data type to the one you like (boolean, numeric and so on) ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Sun, Apr 25, 2010 at 7:52 PM, Matthew Keller <mckellercran@gmail.com>wrote:> Hi all, > > Probably a rudimentary question. I have a flat file that looks like > this (the real one has ~10e6 elements): > > 10110100101001011101011 > > and I want to pull that into R as a vector, but with each digit being > it's own element. There are no separators between the digits. How can > I accomplish this? Thanks in advance! > > Matt > > -- > Matthew C Keller > Asst. Professor of Psychology > University of Colorado at Boulder > www.matthewckeller.com > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Matthew Keller
2010-Apr-25 17:00 UTC
[R] how to make read in a vector of 0s and 1s with no space between them
Hi all, Quickly received an answer off the list. To do this is easy. Pull it in using e.g., scan(). Then use strsplit: z <- '10001011010010' strsplit(z,'') On Sun, Apr 25, 2010 at 10:52 AM, Matthew Keller <mckellercran at gmail.com> wrote:> Hi all, > > Probably a rudimentary question. I have a flat file that looks like > this (the real one has ~10e6 elements): > > 10110100101001011101011 > > and I want to pull that into R as a vector, but with each digit being > it's own element. There are no separators between the digits. How can > I accomplish this? Thanks in advance! > > Matt > > -- > Matthew C Keller > Asst. Professor of Psychology > University of Colorado at Boulder > www.matthewckeller.com >-- Matthew C Keller Asst. Professor of Psychology University of Colorado at Boulder www.matthewckeller.com
Henrik Bengtsson
2010-Apr-25 18:25 UTC
[R] how to make read in a vector of 0s and 1s with no space between them
See readBin(), e.g. r <- readBin(pathname, what="raw", n=10e6); str(r); # raw [1:21] 30 31 30 31 ... c <- rawToChar(x, multiple=TRUE); str(c); # chr [1:21] "0" "1" "0" "1" "0" "0" "1" "0" "1" "0" ... i <- as.integer(r); str(i); # int [1:21] 48 49 48 49 48 48 49 48 49 48 ... /Henrik On Sun, Apr 25, 2010 at 6:52 PM, Matthew Keller <mckellercran at gmail.com> wrote:> Hi all, > > Probably a rudimentary question. I have a flat file that looks like > this (the real one has ~10e6 elements): > > 10110100101001011101011 > > and I want to pull that into R as a vector, but with each digit being > it's own element. There are no separators between the digits. How can > I accomplish this? Thanks in advance! > > Matt > > -- > Matthew C Keller > Asst. Professor of Psychology > University of Colorado at Boulder > www.matthewckeller.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Duncan Murdoch
2010-Apr-25 22:32 UTC
[R] how to make read in a vector of 0s and 1s with no space between them
On 25/04/2010 12:52 PM, Matthew Keller wrote:> Hi all, > > Probably a rudimentary question. I have a flat file that looks like > this (the real one has ~10e6 elements): > > 10110100101001011101011 > > and I want to pull that into R as a vector, but with each digit being > it's own element. There are no separators between the digits. How can > I accomplish this? Thanks in advance!Is it broken up into lines at all? Then read.fwf might be what you want. If it's just a huge collection of 0s and 1s with no line breaks, then use readChar(filename, nchar=1) to read the individual characters, and later convert them to digits using ifelse(), e.g. chars <- readChar(filename, nchar=1) digits <- ifelse(chars=="1", 1, ifelse(chars == "0", 0, NA)) if (any(is.na(digits))) stop("not all 0s and 1s!") Duncan Murdoch
Apparently Analagous Threads
- skip non-sequential lines using scan?
- ideas about how to reduce RAM & improve speed in trying to use lapply(strsplit())
- things that are difficult/impossible to do in SAS or SPSS but simple in R
- how to get remote ESS graphics to work?
- unable to install package ff