【发布时间】:2014-06-17 10:53:29
【问题描述】:
我想用E 字符串在同一个no 列中用多个State 标记样本(sample_id 列)。
我的df 数据框输入:
no sample_id State
chr1-15984544-15996851-0n NE001788 0n
chr1-15984544-15996851-0n NE001788 1n
chr1-15984544-15996851-0n NE001836 0n
chr1-15984544-15996851-0n NE002026 0n
chr1-15984544-15996851-0n NE001413 0n
chr1-15984544-15996851-0n NE001438 0n
我期待的output:
no sample_id State
chr1-15984544-15996851-0n NE001788 E
chr1-15984544-15996851-0n NE001836 0n
chr1-15984544-15996851-0n NE002026 0n
chr1-15984544-15996851-0n NE001413 0n
chr1-15984544-15996851-0n NE001438 0n
样本NE001788 被标记为E,因为它在同一个no 字符串中有两种不同的状态(State)。
我曾将以下代码用于小型数据帧:
df <- read.table(text= 'no sample_id State
chr1-15984544-15996851-0n NE001788 0n
chr1-15984544-15996851-0n NE001788 1n
chr1-15984544-15996851-0n NE001836 0n
chr1-15984544-15996851-0n NE002026 0n
chr1-15984544-15996851-0n NE001413 0n
chr1-15984544-15996851-0n NE001438 0n',header=TRUE)
library(plyr)
output <- unique(ddply(df,.(no,sample_id),mutate,State=if(length(unique(State))>1) {"E"} else State))
它工作正常。但是,我现在有一个大数据框(超过 70 万行)。在这个大数据框中,我得到一个内存错误:cannot allocate vector of size 75kb。
我在这里寻求替代方案以达到相同的结果,而不会出现内存突破。
非常感谢。
【问题讨论】:
-
在您预期的输出中,您删除了重复的行,这是故意的还是错误的?因为您没有在请求中指定
-
是的,我想删除重复的行并只创建一个带有“E”
State的行。在我的输出中,我不希望两个no条目指向同一个sample_id。当我收到多个条目时,我想创建“E”行。
标签: r memory-management plyr