Friday, January 20, 2023

Read Write File Binary Pada Programming-R

 


1. Write File Binary

Format : writeBin(object,con)

keterangan :

  • con : objek koneksi yang digunakan untuk menulis file biner.
  • objek : file biner tempat kita menulis data. 


Contoh :

# Reading data "mtcars" sebagai file csv dan hanya akan menyimpan kolom "cyl", "am", dan "gear"..  

write.table(mtcars, file = "mtcars.csv",row.names = FALSE, na = "",   

col.names = TRUE, sep = ",")  

# Storing 5 records from the csv file as a new data frame.  

new.mtcars <- read.table("mtcars.csv",sep = ",",header = TRUE,nrows = 5)  

new.mtcars  

# Creating a connection object to write the binary file using mode "wb".  

write.filename = file("/Users/R/binary.bin", "wb")  

# Writing the column names of the data frame to the connection object.  

writeBin(colnames(new.mtcars), write.filename)  

# Writing the records in each of the column to the file.  

writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear), write.filename)  

# Closing the file for writing so that other programs can read it.  

close(write.filename)   

   


2. Read File Binary

Format : readBin(con,what,n) 

Keterangan :

  • con : objek koneksi yang digunakan untuk membaca file biner.
  • what : mode seperti karakter, integer, dll yang mewakili byte untuk dibaca.
  • n  : jumlah byte yang ingin dibaca dari file biner.


Contoh :

# Creating a connection object to read the file in binary mode using "rb".  

read.filename <- file("/Users/R/binary.bin", "rb")  

# Reading the column names. n = 3 as we have 3 columns.  

column.names <- readBin(read.filename, character(),  n = 3)  

# Reading the column values. n = 18 as we have 3 column names and 15 values.  

read.filename <- file("/Users//R/binary.bin", "rb")  

bin_data <- readBin(read.filename, integer(),  n = 18)  

# Printing the data.  

print(bin_data)  

# Reading the values from 4th byte to 8th byte, which represents "cyl."  

cyl_data = bin_data[4:8]  

print(cyl_data)  

# Reading the values form 9th byte to 13th byte which represents "am".  

am_data = bin_data[9:13]  

print(am_data)  

# Reading the values form 9th byte to 13th byte which represents "gear".  

gear_data = bin_data[14:18]  

print(gear_data)  

# Combining all the read values to a dat frame.  

final_data = cbind(cyl_data, am_data, gear_data)  

colnames(final_data) = column.names  

print(final_data)  



Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...