R语言二进制文件

Python09

R语言二进制文件,第1张

R语言二进制文件

二进制文件是一个文件,其中包含仅以位和字节形式存储的信息(0和1)。它们不可读,因为其中的字节转换为包含许多其他不可打印字符的字符和符号。尝试使用任何文本编辑器读取二进制文件将显示为类似?和?这样的字符。

二进制文件必须由特定程序读取才能使用。例如,Microsoft Word程序的二进制文件只能通过Word程序读取到人类可读的形式。这表明,除了人类可读的文本之外,还有更多的信息,如格式化的字符和页码等,它们也与字母数字字符一起存储。最后二进制文件是一个连续的字节序列。 我们在文本文件中看到的换行符是将第一行连接到下一个的字符。

有时,由其他程序生成的数据需要由R作为二进制文件处理。 另外R需要创建可以与其他程序共享的二进制文件。

R有两个函数用来创建和读取二进制文件,它们分别是:WriteBin()和readBin()函数。

语法

writeBin(object,con)readBin(con,what,n)

R

以下是使用的参数的描述 -

con- 是要读取或写入二进制文件的连接对象。

object- 是要写入的二进制文件。

what- 是像字符,整数等的模式,代表要读取的字节。

n- 是从二进制文件读取的字节数。

实例

这里考虑使用R内置数据“mtcars”。 首先,我们从它创建一个csv文件并将其转换为二进制文件并将其存储为操作系统文件。接下来将这个二进制文件读入R中。

1. 写入二进制文件

我们将数据帧“mtcars”读为csv文件,然后将其作为二进制文件写入操作系统。参考以下代码实现 -

# Read the "mtcars" data frame as a csv file and store only the columns"cyl","am"and"gear".write.table(mtcars,file="mtcars.csv",row.names=FALSE,na="",col.names=TRUE,sep=",")# Store 5 records from the csv file as a new data frame.new.mtcars<-read.table("mtcars.csv",sep=",",header=TRUE,nrows=5)# Create a connection object to write the binary file using mode "wb".write.filename=file("/web/com/binmtcars.dat","wb")# Write the column names of the data frame to the connection object.writeBin(colnames(new.mtcars),write.filename)# Write the records in each of the column to the file.writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear),write.filename)# Close the file for writing so that it can be read by other program.close(write.filename)

R2. 读取二进制文件

上面创建的二进制文件将所有数据作为连续字节存储。 因此,我们将通过选择列名称和列值的适当值来读取它。

# Create a connection object to read the file in binary mode using "rb".read.filename<-file("/web/com/binmtcars.dat","rb")# First read the column names. n = 3 as we have 3 columns.column.names<-readBin(read.filename,character(),n=3)# Next read the column values. n = 18 as we have 3 column names and 15 values.read.filename<-file("/web/com/binmtcars.dat","rb")bindata<-readBin(read.filename,integer(),n=18)# Print the data.print(bindata)# Read the values from 4th byte to 8th byte which represents "cyl".cyldata=bindata[4:8]print(cyldata)# Read the values form 9th byte to 13th byte which represents "am".amdata=bindata[9:13]print(amdata)# Read the values form 9th byte to 13th byte which represents "gear".geardata=bindata[14:18]print(geardata)# Combine all the read values to a dat frame.finaldata=cbind(cyldata,amdata,geardata)colnames(finaldata)=column.namesprint(finaldata)

R

当我们执行上面的代码,它产生以下结果和图表 -

[1]7108963 17280812497496037 6 6 4 [7] 6 8 1 1 1 0[13] 0 4 4 4 3 3[1] 6 6 4 6 8[1] 1 1 1 0 0[1] 4 4 4 3 3 cyl am gear[1,] 6 14[2,] 6 14[3,] 4 14[4,] 6 03[5,] 8 03

Shell

我们可以看到,通过读取R中的二进制文件,得到了原始数据。

这是我以前学习C语言时做过的,希望对你有所帮助!

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

int shoudsave=0/* */

struct student

{

char num[10]/* 学号 */

char name[20]

char sex[4]

int cgrade

int mgrade

int egrade

int totle

int ave

char neartime[10]/* 最近更新时间 */

}

typedef struct node

{

struct student data

struct node *next

}Node,*Link

void menu()

{

printf("********************************************************************************")

printf("\t1登记学生资料\t\t\t\t\t2删除学生资料\n")

printf("\t3查询学生资料\t\t\t\t\t4修改学生资料\n")

printf("\t5保存学生资料\t\t\t\t\t0退出系统\n")

printf("********************************************************************************\n")

}

void printstart()

{

printf("-----------------------------------------------------------------------\n")

}

void Wrong()

{

printf("\n=====>提示:输入错误!\n")

}

void Nofind()

{

printf("\n=====>提示:没有找到该学生!\n")

}

void printc() /* 本函数用于输出中文 */

{

printf(" 学号\t 姓名 性别 英语成绩 数学成绩 C语言成绩 总分 平均分\n")

}

void printe(Node *p)/* 本函数用于输出英文 */

{

printf("%-12s%s\t%s\t%d\t%d\t%d\t %d\t %d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave)

}

Node* Locate(Link l,char findmess[],char nameornum[]) /* 该函数用于定位连表中符合要求的接点,并返回该指针 */

{

Node *r

if(strcmp(nameornum,"num")==0) /* 按学号查询 */

{

r=l->next

while(r!=NULL)

{

if(strcmp(r->data.num,findmess)==0)

return r

r=r->next

}

}

else if(strcmp(nameornum,"name")==0) /* 按姓名查询 */

{

r=l->next

while(r!=NULL)

{

if(strcmp(r->data.name,findmess)==0)

return r

r=r->next

}

}

return 0

}

void Add(Link l) /* 增加学生 */

{

Node *p,*r,*s

char num[10]

r=l

s=l->next

while(r->next!=NULL)

r=r->next/* 将指针置于最末尾 */

while(1)

{

printf("请你输入学号(以'0'返回上一级菜单:)")

scanf("%s",num)

if(strcmp(num,"0")==0)

break

while(s)

{

if(strcmp(s->data.num,num)==0)

{

printf("=====>提示:学号为'%s'的学生已经存在,若要修改请你选择'4 修改'!\n",num)

printstart()

printc()

printe(s)

printstart()

printf("\n")

return

}

s=s->next

}

p=(Node *)malloc(sizeof(Node))

strcpy(p->data.num,num)

printf("请你输入姓名:")

scanf("%s",p->data.name)

getchar()

printf("请你输入性别:")

scanf("%s",p->data.sex)

getchar()

printf("请你输入c语言成绩:")

scanf("%d",&p->data.cgrade)

getchar()

printf("请你输入数学成绩:")

scanf("%d",&p->data.mgrade)

getchar()

printf("请你输入英语成绩:")

scanf("%d",&p->data.egrade)

getchar()

p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade

p->data.ave=p->data.totle / 3

/* 信息输入已经完成 */

p->next=NULL

r->next=p

r=p

shoudsave=1

}

}

void Qur(Link l) /* 查询学生 */

{

int sel

char findmess[20]

Node *p

if(!l->next)

{

printf("\n=====>提示:没有资料可以查询!\n")

return

}

printf("\n=====>1按学号查找\n=====>2按姓名查找\n")

scanf("%d",&sel)

if(sel==1)/* 学号 */

{

printf("请你输入要查找的学号:")

scanf("%s",findmess)

p=Locate(l,findmess,"num")

if(p)

{

printf("\t\t\t\t查找结果\n")

printstart()

printc()

printe(p)

printstart()

}

else

Nofind()

}

else if(sel==2) /* 姓名 */

{

printf("请你输入要查找的姓名:")

scanf("%s",findmess)

p=Locate(l,findmess,"name")

if(p)

{

printf("\t\t\t\t查找结果\n")

printstart()

printc()

printe(p)

printstart()

}

else

Nofind()

}

else

Wrong()

}

void Del(Link l) /* 删除 */

{

int sel

Node *p,*r

char findmess[20]

if(!l->next)

{

printf("\n=====>提示:没有资料可以删除!\n")

return

}

printf("\n=====>1按学号删除\n=====>2按姓名删除\n")

scanf("%d",&sel)

if(sel==1)

{

printf("请你输入要删除的学号:")

scanf("%s",findmess)

p=Locate(l,findmess,"num")

if(p)

{

r=l

while(r->next!=p)

r=r->next

r->next=p->next

free(p)

printf("\n=====>提示:该学生已经成功删除!\n")

shoudsave=1

}

else

Nofind()

}

else if(sel==2)

{

printf("请你输入要删除的姓名:")

scanf("%s",findmess)

p=Locate(l,findmess,"name")

if(p)

{

r=l

while(r->next!=p)

r=r->next

r->next=p->next

free(p)

printf("\n=====>提示:该学生已经成功删除!\n")

shoudsave=1

}

else

Nofind()

}

else

Wrong()

}

void Modify(Link l)

{

Node *p

char findmess[20]

if(!l->next)

{

printf("\n=====>提示:没有资料可以修改!\n")

return

}

printf("请你输入要修改的学生学号:")

scanf("%s",findmess)

p=Locate(l,findmess,"num")

if(p)

{

printf("请你输入新学号(原来是%s):",p->data.num)

scanf("%s",p->data.num)

printf("请你输入新姓名(原来是%s):",p->data.name)

scanf("%s",p->data.name)

getchar()

printf("请你输入新性别(原来是%s):",p->data.sex)

scanf("%s",p->data.sex)

printf("请你输入新的c语言成绩(原来是%d分):",p->data.cgrade)

scanf("%d",&p->data.cgrade)

getchar()

printf("请你输入新的数学成绩(原来是%d分):",p->data.mgrade)

scanf("%d",&p->data.mgrade)

getchar()

printf("请你输入新的英语成绩(原来是%d分):",p->data.egrade)

scanf("%d",&p->data.egrade)

p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade

p->data.ave=p->data.totle/3

printf("\n=====>提示:资料修改成功!\n")

shoudsave=1

}

else

Nofind()

}

void Disp(Link l)

{

int count=0

Node *p

p=l->next

if(!p)

{

printf("\n=====>提示:没有资料可以显示!\n")

return

}

printf("\t\t\t\t显示结果\n")

printstart()

printc()

printf("\n")

while(p)

{

printe(p)

p=p->next

}

printstart()

printf("\n")

}

void Tongji(Link l)

{

Node *pm,*pe,*pc,*pt,*pa/* 用于指向分数最高的接点 */

Node *r=l->next

if(!r)

{

printf("\n=====>提示:没有资料可以统计!\n")

return

}

pm=pe=pc=pt=pa=r

while(r!=NULL)

{

if(r->data.cgrade>=pc->data.cgrade)

pc=r

if(r->data.mgrade>=pm->data.mgrade)

pm=r

if(r->data.egrade>=pe->data.egrade)

pe=r

if(r->data.totle>=pt->data.totle)

pt=r

if(r->data.ave>=pa->data.ave)

pa=r

r=r->next

}

printf("------------------------------统计结果--------------------------------\n")

printf("总分最高者:\t%s %d分\n",pt->data.name,pt->data.totle)

printf("平均分最高者:\t%s %d分\n",pa->data.name,pa->data.ave)

printf("英语最高者:\t%s %d分\n",pe->data.name,pe->data.egrade)

printf("数学最高者:\t%s %d分\n",pm->data.name,pm->data.mgrade)

printf("c语言最高者:\t%s %d分\n",pc->data.name,pc->data.cgrade)

printstart()

}

void Sort(Link l)

{

Link ll

Node *p,*rr,*s

ll=(Link)malloc(sizeof(Node))/* 用于做新的连表 */

ll->next=NULL

if(l->next==NULL)

{

printf("\n=====>提示:没有资料可以排序!\n")

return

}

p=l->next

while(p)

{

s=(Node*)malloc(sizeof(Node))/* 新建接点用于保存信息 */

s->data=p->data

s->next=NULL

rr=ll

while(rr->next!=NULL &&rr->next->data.totle>=p->data.totle)

rr=rr->next

if(rr->next==NULL)

rr->next=s

else

{

s->next=rr->next

rr->next=s

}

p=p->next

}

free(l)

l->next=ll->next

printf("\n=====>提示:排序已经完成!\n")

}

void Save(Link l)

{

FILE* fp

Node *p

int flag=1,count=0

fp=fopen("c:\\student","wb")

if(fp==NULL)

{

printf("\n=====>提示:重新打开文件时发生错误!\n")

exit(1)

}

p=l->next

while(p)

{

if(fwrite(p,sizeof(Node),1,fp)==1)

{

p=p->next

count++

}

else

{

flag=0

break

}

}

if(flag)

{

printf("\n=====>提示:文件保存成功.(有%d条记录已经保存.)\n",count)

shoudsave=0

}

fclose(fp)

}

void main()

{

Link l/* 连表 */

FILE *fp/* 文件指针 */

int sel

char ch

char jian

int count=0

Node *p,*r

printf("\t\t\t\t学生成绩管理系统\n\t\t\t\t\n")

l=(Node*)malloc(sizeof(Node))

l->next=NULL

r=l

fp=fopen("C:\\student","rb")

if(fp==NULL)

{

printf("\n=====>提示:文件还不存在,是否创建?(y/n)\n")

scanf("%c",&jian)

if(jian=='y'||jian=='Y')

fp=fopen("C:\\student","wb")

else

exit(0)

}

printf("\n=====>提示:文件已经打开,正在导入记录......\n")

while(!feof(fp))

{

p=(Node*)malloc(sizeof(Node))

if(fread(p,sizeof(Node),1,fp)) /* 将文件的内容放入接点中 */

{

p->next=NULL

r->next=p

r=p/* 将该接点挂入连中 */

count++

}

}

fclose(fp)/* 关闭文件 */

printf("\n=====>提示:记录导入完毕,共导入%d条记录.\n",count)

while(1)

{

menu()

printf("请你选择操作:")

scanf("%d",&sel)

if(sel==0)

{

if(shoudsave==1)

{ getchar()

printf("\n=====>提示:资料已经改动,是否将改动保存到文件中(y/n)?\n")

scanf("%c",&ch)

if(ch=='y'||ch=='Y')

Save(l)

}

printf("\n=====>提示:你已经退出系统,再见!\n")

break

}

switch(sel)

{

case 1:Add(l)break/* 增加学生 */

case 2:Del(l)break/* 删除学生 */

case 3:Qur(l)break/* 查询学生 */

case 4:Modify(l)break/* 修改学生 */

case 5:Save(l)break/* 保存学生 */

case 9:printf("\t\t\t==========帮助信息==========\n")break

default: Wrong()getchar()break

}

}

}

1,ubuntu下安装sublime text3

打开终端输入:

sudo add-apt-repository ppa:webupd8team/sublime-text-3#添加sublime text 3仓库

更新库:

sudoapt-get update #更新库

安装sublime text 3 :

sudoapt-get install sublime-text-installer #安装sublime text 3

2,安装packagecontrol

下面就需要安装能运行R的插件啦,安装之前可以先安装packagecontrol具体方法如下

打开sublime text 3

通过快捷键ctrl+` 或者 View >Show Console菜单打开控制台

import urllib.request,ospf='PackageControl.sublime-package'ipp=sublime.installed_packages_path()urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()))open(os.path.join(ipp,pf),'wb').write(urllib.request.urlopen('http:// sublime.wbond.net/'+pf.replace('',' ')).read())

粘贴以上代码回车就好了。

3安装packages(R-Box和sublimeREPL):

sublime text 3中按下快捷键Ctrl+Shift+P调出命令面板,输入install选择installPackage选项回车,然后输入R-Box包,回车安装,然后同样的方法安装sublimeREPL包,安装好这两个包,恭喜你已经具备在sublime中运行的必要条件了,下面就是修改调试的过程了,

首先Ctrl+Shift+P调出命令面板输入R-Box:Choose Program 然后看见了sublimeREPL,ok就是选择这个。

然后菜单栏tools->sublimeREPL->R,此时R语言的运行窗口出来了,然后Alt+Shift+2实现左右分屏为两个栏,把R语言窗口调到右边就好了。

最后实践在代码框选中一段代码Ctrl+Enter,神奇的代码在右边出现啦,Ctrl+\是快捷设置R的工作空间为你代码所在的地方,超级方便。

好了赶快实践起来吧,感受sublime text 3带你飞的感觉,对了,还有就是每次启动时候要重新启动下sublimeREPL R这个没解决,以后解决的再发上来。