<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>生信菜鸟团 &#187; 数据框</title>
	<atom:link href="http://www.bio-info-trainee.com/tag/%e6%95%b0%e6%8d%ae%e6%a1%86/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bio-info-trainee.com</link>
	<description>欢迎去论坛biotrainee.com留言参与讨论，或者关注同名微信公众号biotrainee</description>
	<lastBuildDate>Sat, 28 Jun 2025 14:30:13 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.33</generator>
	<item>
		<title>R语言-比较数据框提取列的速度</title>
		<link>http://www.bio-info-trainee.com/952.html</link>
		<comments>http://www.bio-info-trainee.com/952.html#comments</comments>
		<pubDate>Fri, 28 Aug 2015 05:52:05 +0000</pubDate>
		<dc:creator><![CDATA[ulwvfje]]></dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[cor]]></category>
		<category><![CDATA[数据框]]></category>
		<category><![CDATA[相关系数]]></category>

		<guid isPermaLink="false">http://www.bio-info-trainee.com/?p=952</guid>
		<description><![CDATA[结论：从数据框里面取某列数据，三种方法的时间消耗区别很大，直接用索引值，是最快的 &#8230; <a href="http://www.bio-info-trainee.com/952.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>结论：从数据框里面取某列数据，三种方法的时间消耗区别很大，直接用索引值，是最快的，而用$符号其次，用列名最慢。</p>
<p>我在R里面建立了一个表达量矩阵，列名是一个个样品，行是一个个探针，矩阵值是该探针在该样品测定的表达量。</p>
<p>那么，如果我要看看名为"202723_s_at"的探针的表达向量与其它所有探针的表达向量的相关系数，我可以用以下三种方法：</p>
<p>&gt; system.time(apply(all_dat_t,2,function(x)  cor(all_dat_t$"202723_s_at",x)))</p>
<p>user  system elapsed</p>
<p>22.93    0.03   23.03</p>
<p>&gt; system.time(apply(all_dat_t,2,function(x)  cor(all_dat_t[,"202723_s_at"],x)))</p>
<p>Timing stopped at: 92.02 5.32 97.66</p>
<p>太耗时间了，省去</p>
<p>&gt; system.time(apply(all_dat_t,2,function(x)  cor(all_dat_t[,grep(prob,names(all_dat_t))],x)))</p>
<p>Timing stopped at: 13.55 0.04 13.66</p>
<p>&gt; prob_num=grep(prob,names(all_dat_t))</p>
<p>&gt; system.time(apply(all_dat_t,2,function(x)  cor(all_dat_t[,prob_num],x)))</p>
<p>user  system elapsed</p>
<p>8.14    0.01    8.17</p>
<p>可以看出，如果我首先根据探针名，grep出它在该表达量矩阵的列数，然后用列数来提取它的表达量是最快的，而且时间改善非常明显！</p>
<p>我们再探究一下cor函数的效率问题</p>
<p>探究的矩阵有54675个变量，每个变量均有189个观测值，如果取这个大矩阵的部分变量来求相关系数，结果如下！</p>
<p>&gt; system.time(cor(all_dat_t[,1:10]))</p>
<p>user  system elapsed</p>
<p>0.001   0.000   0.001</p>
<p>&gt; system.time(cor(all_dat_t[,1:100]))</p>
<p>user  system elapsed</p>
<p>0.003   0.000   0.003</p>
<p>&gt; system.time(cor(all_dat_t[,1:1000]))</p>
<p>user  system elapsed</p>
<p>0.107   0.002   0.108</p>
<p>&gt; system.time(cor(all_dat_t[,1:10000]))</p>
<p>user  system elapsed</p>
<p>11.102   0.849  11.983</p>
<p>&gt; system.time(cor(all_dat_t)) 约六分钟也是可以搞定的</p>
<p>但是如果cor(all_dat_t)，六分钟后得到的相关系数矩阵约32G，非常恐怖！</p>
<p>但是它很明显没有把这个32G相关系数矩阵存储到内存，因为我的机器本来就16G内存。我至今不能明白R具体实现机理</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bio-info-trainee.com/952.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
