{"id":2424,"date":"2022-06-29T21:19:16","date_gmt":"2022-06-29T13:19:16","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=2422"},"modified":"2022-06-29T21:19:16","modified_gmt":"2022-06-29T13:19:16","slug":"r%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7-%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=2424","title":{"rendered":"R\u4f7f\u7528\u6280\u5de7-\u7f16\u7a0b"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>title: &#8220;R example&#8221;<br>author:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5218\u94c1\u67f1<br>documentclass: ctexart<br>keywords:<\/li><li>\u4e2d\u6587<\/li><li>R Markdown<br>output:<br>rticles::ctex:<br>fig_caption: yes<br>number_sections: yes<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">toc: yes<\/h2>\n\n\n\n<h1 class=\"wp-block-heading\">\u6570\u636e\u8f93\u5165\u4e0e\u521b\u5efa\u6570\u636e\u6846<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#C()\u7cfb\u5217\u7684\u521b\u5efa\nscores &lt;- c(61,66,90,88,100)\nscores\n\n#data.frame \u521b\u5efa\u6570\u636e\u6846\npoints &lt;- data.frame(\n     labels=c(\"Low\",\"Mid\",\"High\"),\n     ubound = c(0.674,1.64,2.33),\n     lbound = c(0,0.6,1.64)\n )\npoints\n#edit data.frame--\u8868\u683c\u65b9\u5f0f\u8f93\u5165\u6570\u636e\nscore &lt;- data.frame()\nscore &lt;- edit(score)\n#plot\n#plot(cars)\n#-------------\n# scan() \u8f93\u5165\u6570\u636e\uff08\u6570\u5b57\uff09\nx &lt;-scan()\nx<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u6570\u636e\u6846\u5217\u6c42\u548c\u65b9\u6cd5<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>widgets &lt;- c(179,153,183,153,154)\ngadgets &lt;- c(167,193,190,161,181)\nthingys &lt;- c(182,166,170,171,186)\ndaily.prod &lt;- data.frame(widgets,gadgets,thingys,row.names = c('Mon','Tue','Wed','Thu','Fri'))\nrbind(daily.prod,Total=colSums(daily.prod))\ncbind(daily.prod,Totol=rowSums(daily.prod))<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">digits\u5c0f\u6570\u4f4d\u63a7\u5236<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>pi\noptions(digits = 15)\nprint(pi,digits = 4)  #4\u4f4d\u6570\u5b57\nprint(100*pi,digits = 4)\ncat(pi,\"\\n\")\ncat(format(pi,digits = 4),\"\\n\") #4\u4f4d\u6570\u5b57\npnorm(-3:3)\nprint(pnorm(-3:3),digits = 3)<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u8f93\u51fa\u5230\u6587\u4ef6<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#cat\u4f7f\u7528\n#cat(\"The answer is\",answer,file = \"filename\",append = TRUE)\n#cat(data,file = \"filename\",append = TRUE)\n#sink\uff08\uff09\u4f7f\u7528\n#sink(\"filename\")   #\u5c4f\u5e55\u8f93\u51fa\u88ab\u5199\u5165\u6587\u4ef6\n    #    ...other session work...\n    #source(\"script.R\")\n#sink() #\u505c\u6b62\u5199\u5165\u6587\u4ef6\uff0c\u8f93\u51fa\u5230\u5c4f\u5e55\n\n#\u6587\u4ef6\u5217\u8868\uff0c\u76ee\u5f55\u5217\u8868\nlist.files()\n#list.dirs()\n\n#write \u628a\u6570\u636e\u5199\u5165\u6587\u4ef6\n##write(x,\"filename\")\n#write.csv(data,file = \"filename\",row.names = T)<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u8bfb\u53d6\u6570\u636e<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code># records &lt;- read.fwf(\"\",widths = c(w1,w2,w3,...,w)) #\u56fa\u5b9a\u5bbd\u5ea6\u7684\u6570\u636e\u683c\u5f0fw-\u5bbd\u5ea6\n# dfram &lt;- read.table(\"data.txt\",header=TRUE,sep = \":\")\n# tbl &lt;- read.csv(\"filename\",header=FALSE)\n# tbl &lt;- read.table(\"http:\/\/www.example.com\/download\/data.txt\")  #\u76f4\u63a5\u8bfb\u53d6\u7f51\u7edc\u6570\u636e\n# \n# save(mydata,file = \"mydata.RData\")  # \u5b58\u5165\n# load(\"mydata.RData\")                # \u8bfb\u51fa<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e0d\u540c\u81ea\u7531\u7684\u7684t\u5206\u5e03\u56fe<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#pdf(\"t-distribution.pdf\")\npar(mfrow=c(2,2))\n#opar=par(no.readonly = TRUE)\nx=seq(-4,4,length.out=100)\n\npar(fig=c(0,1,0,1))\nd1=dt(x,2)\nd2=dt(x,5)\nd3=dt(x,15)\n#\u4e0d\u540c\u81ea\u7531\u5ea6t\u5206\u5e03\u5bc6\u5ea6\u66f2\u7ebf\u753b\u5728\u4e00\u5f20\u56fe\u4e0a plot\u548clines\u7ed3\u5408\u5373\u53ef\nplot(d1,ylim = c(0,0.4),type=\"l\")\nlines(d2,col='red')\nlines(d3,col=\"blue\")\n#\u4e0d\u540c\u81ea\u7531\u5ea6t\u5206\u5e03\u5bc6\u5ea6\u66f2\u7ebf\u753b\u5728\u4e0d\u540c\u7684\u56fe\u4e0a \nplot(x,dt(x,1),type = \"l\",ylim = c(0,0.5))\nplot(x,dt(x,3),type = \"l\",ylim = c(0,0.5))\nplot(x,dt(x,5),type = \"l\",ylim = c(0,0.5))\nplot(x,dt(x,10),type = \"l\",ylim = c(0,0.5))\nplot(x,dt(x,30),type = \"l\",ylim = c(0,0.5))\n#dev.off()<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Cluster\u805a\u7c7b\u5206\u6790<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>means &lt;- sample(c(-3,0,3),replace = T)\nx &lt;- rnorm(99,mean = means)\nd &lt;- dist(x)\nhc &lt;- hclust(d)\nplot(hc,hang=-1)\nclust &lt;- cutree(hc,k=3)\nplot(x~factor(clust),main = \"Class Mean\")<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u51e0\u4e2aR\u51fd\u6570 list which apply lapply sapply scan cat file<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">list()\u5217\u8868<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>findwords &lt;- function(tf){     #tf short for:text file\n  txt &lt;- scan(tf,\"\")           #\u8bfb\u53d6\u6587\u672c\u6587\u4ef6tf\u5185\u5bb9\uff0c\u7a7a\u683c\u95f4\u9694\u7684\u82f1\u6587\u5185\u5bb9\uff0c\u6c49\u5b57\u4e0d\u9002\u7528\n  #print(txt)                   #\u8f93\u51fa\u6587\u672c\u5217\u8868\n  wl &lt;- list()                 #\u521b\u5efaword\uff08\u5355\u8bcd\uff09\u5217\u8868list --wl\n  for (i in 1:length(txt)){    # \u5faa\u73af\u8bfb\u53d6txt\u5185\u5355\u8bcd\n    wrd &lt;- txt&#91;i]\n    wl&#91;&#91;wrd]] &lt;- c(wl&#91;&#91;wrd]],i)  # wl&#91;&#91;wrd]\u662f\u4e2a\u5355\u8bcd\u4f4d\u7f6e\uff0c\u628a\u4f4d\u7f6e\u5411\u91cf\u5b58\u8d77\u6765\n  }\n  return(wl)\n}\n#orders the output of findwords() by word frequwncy\nfreqwl &lt;- function(wordlist){\n  freqs &lt;- sapply(wordlist,length)\n  return(wordlist&#91;order(freqs)])   #order()\u6309\u4f4d\u6b21\u6392\u5e8f\uff0csort() \u6309\u503c\u6392\u5e8f\n}\n\nx &lt;- findwords(\"tf\") \n#print(x)\n\n# names(x)     #\u663e\u793a\u5217\u8868\u6807\u7b7e\u540d\uff0c\u63d0\u53d6x\u4e2d\u7684\u5355\u8bcd\n# unlisted(x)  #\u83b7\u53d6\u5217\u8868\u7684\u503c\n# unname(x)    #\u53bb\u6389\u5217\u8868\u5143\u7d20\u540d\n#sort(names(x)) #\u5217\u8868\u6807\u7b7e\u540d\uff08\u5355\u8bcd)\u6392\u5e8f\n#\u63d0\u53d6\u8bcd\u9891\u8f83\u9ad8\u768410%\u5355\u8bcd\u4f5c\u56fe\n#----------------------------\nsnyt &lt;- freqwl(x)\n#print(snyt)\nnword &lt;- length(snyt)\n#class(nword)\nfreqs &lt;- sapply(snyt&#91;round(0.9*nword):nword],length)\nbarplot(freqs)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5217\u8868\u5e94\u7528lapply\uff08\uff09 &amp; sapply\uff08\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>lapply(list(1:6,20:36),median) #\u8f93\u51fa\u4e2d\u4f4d\u65701:6\uff0c20:36\u5217\u8868\nsapply(list(1:6,20:36),median) #\u8f93\u51fa\u4e2d\u4f4d\u65701:6\uff0c20:36\u77e9\u9635<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">which()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># which\u51fd\u6570\n# 1. \u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u7684\u5411\u91cf\u4e0b\u6807\ncy &lt;- c(1,4,3,7,NA)\ncy\nwhich(cy&gt;3)   #\u8fd4\u56de\u4e0b\u6807\ncy&#91;which(cy&gt;3)]   #\u8fd4\u56de\u4e0b\u6807\u5bf9\u5e94\u7684\u503c\n\n# 2.\u6570\u7ec4\u4e2d\u4f7f\u7528\nay &lt;- array(rep(c(4,6,2,1,3,7),2),dim=c(2,3,2))\nay\nwhich(ay&gt;6)\nwhich(ay&gt;6,arr.ind=TRUE)\nay&#91;which(ay&gt;6,arr.ind=TRUE)]\nwhich(ay&gt;6,arr.ind=TRUE,useNames=F)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u636e\u6846\u4e2d\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>studentID &lt;- c(1,2,3,4,5,6)\ngender &lt;- c('M','F','M','M','F','F')\nmath &lt;- c(40,60,70,60,90,60)\nEnglish &lt;- c(98,56,78,93,79,78)\nChinese &lt;- c(86,54,78,90,78,54)\ndata &lt;- data.frame(studentID,gender,math,English,Chinese,stringsAsFactors = F)\ndata\n\nwhich(data&#91;,5]==78)\n#\u6311\u9009\u8bed\u6587\u6210\u7ee9\u4e3a78\u7684\u5b66\u751f\ndata&#91;which(data&#91;,5]==78),]\n#\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/chongbaikaishi\/article\/details\/115765282<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u753b\u997cpie\u56fe<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6570\u636e\u51c6\u5907\ninfo = c(1, 2, 4, 8)\n# \u547d\u540d\nnames = c(\"Google\", \"Runoob\", \"Taobao\", \"Weibo\")\n# \u6d82\u8272\uff08\u53ef\u9009\uff09\ncols = c(\"#ED1C24\",\"#22B14C\",\"#FFC90E\",\"#3f48CC\")\n# \u8ba1\u7b97\u767e\u5206\u6bd4\npiepercent = paste(round(100*info\/sum(info)), \"%\")\n# \u7ed8\u56fe\npie(info, labels=piepercent, main = \"\u7f51\u7ad9\u5206\u6790\", col=cols)\n# \u6dfb\u52a0\u989c\u8272\u6837\u672c\uff08\u56fe\u4f8b\uff09\u6807\u6ce8\nlegend(\"topright\", names, cex=0.8, fill=cols)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed8\u5236\u6c14\u6ce1\u56fe bibole<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>attach(mtcars)\nr &lt;- sqrt(disp\/pi)\nsymbols(wt, mpg, circle=r, inches=0.30,\n        fg=\"white\", bg=\"lightblue\",\n        main=\"Bubble Plot with point size proportional to displacement\",\n        ylab=\"Miles Per Gallon\",\n        xlab=\"Weight of Car (lbs\/1000)\")\ntext(wt, mpg, rownames(mtcars), cex=0.6)\ndetach(mtcars)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">scan and cat<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>rm(list=ls())\n# x &lt;- scan(\"\")    #\u952e\u76d8\u8f93\u5165\u6570\u636ex\n# y &lt;- scan(\"\")    #\u952e\u76d8\u8f93\u5165\u6570\u636ey\nkids &lt;- c(\"Tom\",\"jime\",\"cat\",\"peter\")\nage &lt;- c(23,14,26,25)\nd &lt;- data.frame(kids,age)\nd\nwrite.table(d,'kids.txt')\n#getwd()\nkids &lt;- scan(file = \"kids.txt\",what = \"\") #\u8bfb\u53d6\u6587\u4ef6\u4e2d\u6570\u636e\uff0cwhat=\u2018\u2019 \u5b57\u7b26\uff0c\u9ed8\u8ba4\u8bfb\u53d6\u5b9e\u6570\u3002\nkids\n#scan&amp;cat \u914d\u5408file()\u4f7f\u7528\nf &lt;- file(\"kids\",'w')\ncat(\"12 23 56\",f,append = TRUE) #\u5411\u6587\u4ef6\u4e2d\u8ffd\u52a0\u5199\u5165\nclose(f)<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u5f97\u5230\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>f &lt;- choose.files()\nprint(f)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>title: &#8220;R example&#8221;author: \u5218\u94c1 <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=2424\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2424","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2424"}],"version-history":[{"count":0,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2424\/revisions"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2424"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}