> as.raw(15)
[1] 0f
> rawToBits(as.raw(15))
[1] 01 01 01 01 00 00 00 00
> rawToBits(0f)
Error: unexpected symbol in "rawToBits(0f"
> rawToBits("0f")
Error in rawToBits("0f") : argument 'x' must be a raw vector
> rawToBits("0x0f")
Error in rawToBits("0x0f") : argument 'x' must be a raw vector
I have some problems to ask:
1) is that 0f a raw type data?
2) why rawToBits(as.raw(15)) can not get 11110000? the 15 is not 11110000?
15=0f=1*2^0+1*2^1+1*2^2+1*2^3
What is the meaning of 0 in [1] 01 00 00 00 00 00 00 00 when you input rawToBits(as.raw(1))?
In the manual ,i get a raw vector with entries 0 or 1,what is the meaning ofentries 0 or 1.
Why rawToBits(as.raw(2)) is not 10 00 00 00 00 00 00 00?
Just typing 0f doesn't give you something of type raw.
> str(as.raw(15))
raw 0f
> str(0f)
Error: unexpected symbol in "str(0f"
> str("0f")
chr "0f"
If you want to know what's going on with the bits you could try some other values to get a better idea what is going on
> rawToBits(as.raw(1))
[1] 01 00 00 00 00 00 00 00
> rawToBits(as.raw(2))
[1] 00 01 00 00 00 00 00 00
> rawToBits(as.raw(4))
[1] 00 00 01 00 00 00 00 00
> rawToBits(as.raw(8))
[1] 00 00 00 01 00 00 00 00
> rawToBits(as.raw(1 + 2 + 4 + 8))
[1] 01 01 01 01 00 00 00 00
> rawToBits(as.raw(15))
[1] 01 01 01 01 00 00 00 00
Related
I have a list of raw-vectors named "output". Something like that:
[1] 58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00 00 fe
[1] 58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 01 03 19 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 04 6d 65 74 61 00 00 02 13 00 00 00 03 00 00 00 10 00 00 00 01 00
[1] ...
They have different lenghts and are from the type "raw".
I need a dataframe with one vector in each cell:
ID
vectors
1
58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00 00 fe
2
58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 01 03 19 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 04 6d 65 74 61 00 00 02 13 00 00 00 03 00 00 00 10 00 00 00 01 00
I have tried this:
as.data.frame(output)
#Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 27, 3132, 4141, 4267, 3701, 3943, 5200
df <- data.frame(matrix(unlist(output), nrow=length(output)))
#Warning message:
In matrix(unlist(output), nrow = length(output)) :
data length [32954] is not a sub-multiple or multiple of the number of rows [14]
Is there a way to solve my problem?
You have to use I when creating the data.frame.
output <- list(raw(2), raw(3))
DF <- data.frame(ID=1:2, vectors = I(output))
str(DF)
#'data.frame': 2 obs. of 2 variables:
# $ ID : int 1 2
# $ vectors:List of 2
# ..$ : raw 00 00
# ..$ : raw 00 00 00
# ..- attr(*, "class")= chr "AsIs"
DF
#DF
# ID vectors
#1 1 00, 00
#2 2 00, 00, 00
This can be also done with tibble
library(tibble)
output <- list(raw(2), raw(3))
tibble(ID = 1:2, vectors = output)
# A tibble: 2 x 2
ID vectors
<int> <list>
1 1 <raw [2]>
2 2 <raw [3]>
I have a data frame of integers, which I would like to convert to bits, and then split each resulting bit sequence into separate columns. I figured I could convert my bits to string and then split the strings on whitespace. However, my method does not work.
I create my data frame:
r1 <- c(24,25,27)
r2 <- c(98,102,4)
model.data <- as.data.frame(rbind(r1,r2))
and then I convert int to bits with:
model.data[] <- lapply(model.data, function(x) {
if(!is.na(x)) as.character(paste(intToBits(x), collapse = " ")) else x
})
However, this is where I encounter my first problem. The second row of my data frame now seems to have the same values as the first one:
model.data$V1
[1] "00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
[2] "00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
This shouldn't be the case, since the values in the original data frame differ. It's the same case for all the columns here (V2, V3).
My second problem is that I can't split the columns. I try this method:
for(i in names(model.data)) {
a.split <- strsplit(model.data[[i]], split = " ")
cbind(model.data,a.split)
}
but it does nothing.
Could you help me figure out what I'm doing wrong? Thanks!
Use the function "separate" from tidyr package
library(tidyr)
# Your data
r1 <- c(24,25,27)
r2 <- c(98,102,4)
# Concatenate r1 and r2
r3 <- c(r1,r2)
# Create a dataframe
model.data <- as.data.frame(r3)
# Check type of r3
class(model.data$r3)
# Transform with "intToBits" function
model.data[] <- lapply(model.data, function(x) {
if(!is.na(x)) as.character(paste(intToBits(x), collapse = " ")) else x
})
nb = length(unlist(strsplit(model.data$r3[1]," ")))
# Transform into character
model.data$r3 <- as.character(model.data$r3)
# Use "separate" function from tidyr, generating nb variable
model.data %>% separate(r3, into = paste0("V",seq(1, nb, by = 1)), sep = " ")
You need to apply over both rows and columns, using ifelse. Interestingly, using lapply does not seem to work. Here an example for the apply solution:
r1 <- c(24,25,27)
r2 <- c(98,102,4)
model.data <- as.data.frame(rbind(r1,r2))
model.data[] <- as.data.frame(apply(model.data, c(1,2), function(x) ifelse(!is.na(x), as.character(paste(intToBits(x), collapse = " ")), x)), stringsAsFactors = FALSE)
> model.data
V1
r1 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
V2
r1 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 01 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
V3
r1 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Edit:
To create the new data with one bit per variable, first lapply the strsplit, giving you a nested list as output, then use a nested do.call to first cbind the sublists, then rbind the result:
newCols <- lapply(model.data, function(x) strsplit(x, split = " "))
a.split <- as.data.frame(do.call(rbind, do.call(cbind, newCols)), stringsAsFactors = FALSE)
> a.split
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32
1 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
3 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 00 01 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
R use 32-bit integers, 4 bytes (4*8=32, 1 byte=8 bits),max integer is 2^31-1= 2147483647.
intToBits(125)
[1] 01 00 01 01 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
There are 64 bits ,not 32 bits,32 pairs of 00 or 01(32*2=64) ,why?
in the manual ,a raw vector with entries 0 or 1,what is the meaning of entries 0 or 1?
In my opinion, intToBits(125) will generate
10111110 00000000 00000000 00000000 # it is 32 bits
not
01 00 01 01 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. # it is 64 bits
Function intToBits returns 32 numbers, each 0 or 1.
> length(intToBits(125))
[1] 32
The confusion is caused by the hex representation of the raw type of the output of intToBits.
> intToBits(125)[3]
[1] 01
The raw numbers 0 or 1 are shown as 00 or 01.
> writeBin(1:3, raw(), size=4, endian="little")
[1] 01 00 00 00 02 00 00 00 03 00 00 00
> writeBin(c(1,2,3), raw(), size=4, endian="little")
[1] 00 00 80 3f 00 00 00 40 00 00 40 40
> writeBin(c(1:3), raw(), size=4, endian="big")
[1] 00 00 00 01 00 00 00 02 00 00 00 03
> writeBin(c(1,2,3), raw(), size=4, endian="big")
[1] 3f 80 00 00 40 00 00 00 40 40 00 00
The first and third result is what I expect, but why do I get a different raw vector for a vector specified as c(1,2,3)?
Probably because one is stored as an integer, and the other as a double:
typeof(1:3)
typeof(c(1,2,3))
typeof(c(1L,2L,3L))
Is there a way to see, via hex editor or otherwise, if data in a binary file is aligned or packed, specifically for an HPUX system?
If you know what you are looking for and can recognize it in a hex dump, then you can make informed estimates about whether the data is aligned or not, or packed. But in many ways, your question is unanswerable. Where did the data come from? Why can't you ask the person (driving a program, presumably) how it was created?
If you are asking 'what tools could I use to view the data', then you can consider:
od (octal dump - probably with the -c option too)
hd (hex dump - not always available, and seems to be absent on HP-UX)
sed l (that's a lower-case ell - it means list the data; not a good option unless the majority of the data is plain text)
Or you could do it in Perl. Once upon a long time ago (1987 or so), I wrote a program odx (octal dump in hex - weird) that I continue to use - it gives me a hex dump, 16 bytes per line, plus an image of the printable characters. This example isn't very exciting (odx run on itself - on a Sun Sparc):
Black JL: odx odx | sed 10q
0x0000: 7F 45 4C 46 01 02 01 00 00 00 00 00 00 00 00 00 .ELF............
0x0010: 00 02 00 12 00 00 00 01 00 01 0D 84 00 00 00 34 ...............4
0x0020: 00 00 77 9C 00 00 01 00 00 34 00 20 00 05 00 28 ..w......4. ...(
0x0030: 00 24 00 23 00 00 00 06 00 00 00 34 00 01 00 34 .$.#.......4...4
0x0040: 00 00 00 00 00 00 00 A0 00 00 00 A0 00 00 00 05 ................
0x0050: 00 00 00 00 00 00 00 03 00 00 00 D4 00 00 00 00 ................
0x0060: 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 04 ................
0x0070: 00 00 00 00 00 00 00 01 00 00 00 00 00 01 00 00 ................
0x0080: 00 00 00 00 00 00 22 86 00 00 22 86 00 00 00 05 ......"...".....
0x0090: 00 01 00 00 00 00 00 01 00 00 22 88 00 02 22 88 .........."...".
Black JL: