[R] data frame pointers?
arun
smartpink111 at yahoo.com
Thu Oct 24 05:04:51 CEST 2013
Hi Jonathan,If you look at the str()
str(res)
'data.frame': 2 obs. of 4 variables:
$ gene : chr "gene1" "gene2"
$ case_1:List of 2
..$ : chr "nsyn" "amp"
..$ : chr
$ case_2:List of 2
..$ : chr "del"
..$ : chr
$ case_3:List of 2
..$ : chr
..$ : chr "UTR"
In this case,
capture.output(res,file="test.txt") #should work
But, if you wanted to use ?write.table() and also to substitute zeros, perhaps:
res[,2:4] <- lapply(res[,2:4],function(x) {x1 <-unlist(lapply(x,paste,collapse=","));x1[x1==""] <- 0; x1})
str(res)
#'data.frame': 2 obs. of 4 variables:
# $ gene : chr "gene1" "gene2"
# $ case_1: chr "nsyn,amp" "0"
# $ case_2: chr "del" "0"
# $ case_3: chr "0" "UTR"
write.table(res,"test.txt",sep="\t",quote=FALSE,row.names=FALSE)
A.K.
On Wednesday, October 23, 2013 10:44 PM, Jon BR <jonsleepy at gmail.com> wrote:
Hi Arun,
Your suggestion using dcast is simple and worked splendidly! Unfortunately, the resulting data frame does not play nicely with write.table.
Any idea how to could print this out to a tab-delimited text file, perhaps substituting zeros in for the empty cells?
See the error below:
> write.table(res,"test.txt")
Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, :
unimplemented type 'list' in 'EncodeElement'
Best,
Jonathan
On Wed, Oct 23, 2013 at 9:50 PM, arun <smartpink111 at yahoo.com> wrote:
HI,
>
>You may try:
>library(reshape2)
>df <-
>data.frame(case=c("case_1","case_1","case_2","case_3"), gene=c("gene1","gene1","gene1","gene2"), issue=c("nsyn","amp","del","UTR"), stringsAsFactors=FALSE)
>res <- dcast(df,gene~case,value.var="issue",list)
> res
># gene case_1 case_2 case_3
>#1 gene1 nsyn, amp del
>#2 gene2 UTR
>
>
>A.K.
>
>
>
>On Wednesday, October 23, 2013 7:38 PM, Jon BR <jonsleepy at gmail.com> wrote:
>Hello,
> I've been running several programs in the unix shell, and it's time to
>combine results from several different pipelines. I've been writing shell
>scripts with heavy use of awk and grep to make big text files, but I'm
>thinking it would be better to have all my data in one big structure in R
>so that I can query whatever attributes I like, and print several
>corresponding tables to separate files.
>
>I haven't used R in years, so I was hoping somebody might be able to
>suggest a solution or combinatin of functions that could help me get
>oriented..
>
>Right now, I can import my data into a data frame that looks like this:
>
>df <-
>data.frame(case=c("case_1","case_1","case_2","case_3"),gene=c("gene1","gene1","gene1","gene2"),issue=c("nsyn","amp","del","UTR"))
>> df
> case gene issue
>1 case_1 gene1 nsyn
>2 case_1 gene1 amp
>3 case_2 gene1 del
>4 case_3 gene2 UTR
>
>
>I'd like to cook up some combination of functions/scripting that can
>convert a table like df to produce a list or a data frame/ matrix that
>looks like df2:
>
>> df2
> case_1 case_2 case_3
>gene1 nsyn,amp del 0
>gene2 0 0 UTR
>
>I can build df2 manually, like this:
>df2
><-data.frame(case_1=c("nsyn,amp","0"),case_2=c("del","0"),case_3=c("0","UTR"))
>rownames(df2)<-c("gene1","gene2")
>
>but obviously do not want to do this by hand; I want R to generate df2 from
>df.
>
>Any pointers/ideas would be most welcome!
>
>Thanks,
>Jonathan
>
> [[alternative HTML version deleted]]
>
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list