Hi, Try: dat1 <- read.table(text="a b c x 1 4 7 y 2 5 8 z 3 6 9",header=TRUE) dat2 <- dat1 #either library(reshape2) res1 <- within(melt(dat1,id.var="ID"),cell<-as.character(interaction(variable,ID,sep="_")))[,c(4,3)] #or indx <- which(dat2>0,arr.ind=TRUE) res2 <- data.frame(cell=paste(colnames(dat2)[indx[,2]],rownames(dat2)[indx[,1]],sep="_"),value=dat2[indx],stringsAsFactors=FALSE) ?identical(res1,res2) #[1] TRUE A.K. On Tuesday, November 19, 2013 11:07 PM, Nitisha jha <nitisha999 at gmail.com> wrote: Hi could you please let me know the solution? Thanks On Wed, Nov 20, 2013 at 4:13 AM, <smartpink111 at yahoo.com> wrote: Hi,>If this is a data.frame(), then I have some solution for this problem. ?Also, you need to post it in R-help if that is the case. >Thanks. >A.K. ><quote author='nuts'> >Sample input dataset where a,b,c are col names. x y z are row names. >? ? ? ? a ? ? ? b ? ? ? c >x ? ? ? 1 ? ? ? 4 ? ? ? 7 >y ? ? ? 2 ? ? ? 5 ? ? ? 8 >z ? ? ? 3 ? ? ? 6 ? ? ? 9 > >I want output to be like this after manipulating the dataset. >cell ? ?value >a_x ? ? 1 >a_y ? ? 2 >a_z ? ? 3 >b_x ? ? 4 >b_y ? ? 5 >b_z ? ? 6 >c_x ? ? 7 >c_y ? ? 8 >c_z ? ? 9 >where cell and value are the colnames. >How do I achieve this? I am at a loss here. > > ></quote> >Quoted from: >http://r.789695.n4.nabble.com/Datatable-manipulation-tp4680747.html > > >_____________________________________ >Sent from http://r.789695.n4.nabble.com > >
Thanks a lot. It worked :) On Wed, Nov 20, 2013 at 9:44 AM, arun <smartpink111@yahoo.com> wrote:> Hi, > Try: > dat1 <- read.table(text="a b c > x 1 4 7 > y 2 5 8 > z 3 6 9",header=TRUE) > dat2 <- dat1 > #either > library(reshape2) > res1 <- > within(melt(dat1,id.var="ID"),cell<-as.character(interaction(variable,ID,sep="_")))[,c(4,3)] > > #or > indx <- which(dat2>0,arr.ind=TRUE) > res2 <- > data.frame(cell=paste(colnames(dat2)[indx[,2]],rownames(dat2)[indx[,1]],sep="_"),value=dat2[indx],stringsAsFactors=FALSE) > identical(res1,res2) > #[1] TRUE > > A.K. > > > > On Tuesday, November 19, 2013 11:07 PM, Nitisha jha <nitisha999@gmail.com> > wrote: > > Hi > could you please let me know the solution? > Thanks > > > > > On Wed, Nov 20, 2013 at 4:13 AM, <smartpink111@yahoo.com> wrote: > > Hi, > >If this is a data.frame(), then I have some solution for this problem. > Also, you need to post it in R-help if that is the case. > >Thanks. > >A.K. > ><quote author='nuts'> > >Sample input dataset where a,b,c are col names. x y z are row names. > > a b c > >x 1 4 7 > >y 2 5 8 > >z 3 6 9 > > > >I want output to be like this after manipulating the dataset. > >cell value > >a_x 1 > >a_y 2 > >a_z 3 > >b_x 4 > >b_y 5 > >b_z 6 > >c_x 7 > >c_y 8 > >c_z 9 > >where cell and value are the colnames. > >How do I achieve this? I am at a loss here. > > > > > ></quote> > >Quoted from: > >http://r.789695.n4.nabble.com/Datatable-manipulation-tp4680747.html > > > > > >_____________________________________ > >Sent from http://r.789695.n4.nabble.com > > > > >[[alternative HTML version deleted]]
Hi, Try: dat1 <- read.table(text="a ??? b ??? c ??? d?????? e 1 ??? 2 ??? 3 ??? 4 ??? 5 10 ??? 9 ??? 8 ??? 7 ??? 6",sep="",header=TRUE) Names1<- read.table(text="Original? ??? New??? e ??? ee b ??? bb??? a ??? aa c ??? cc d ??? dd",sep="",header=TRUE,stringsAsFactors=FALSE) It is better to dput() your dataset.? For example: ?dput(Names1) structure(list(Original = c("e", "b", "a", "c", "d"), New = c("ee", "bb", "aa", "cc", "dd")), .Names = c("Original", "New"), class = "data.frame", row.names = c(NA, -5L)) ?names(dat1) <- Names1[,2][match(names(dat1), Names1[,1])] ## ?dat1 #? aa bb cc dd ee #1? 1? 2? 3? 4? 5 #2 10? 9? 8? 7? 6 A.K. On Thursday, November 21, 2013 1:45 PM, Nitisha jha <nitisha999 at gmail.com> wrote: Hi, Thanks. I used as.character() and got the right strings. Btw, I have lots of handicaps regarding R. I have to rename the columns(I have 22 columns here). I have the new names along with the original names in another dataset. Right now, I am going hardcoding all the? 19 name changes(tedious and not optimum). 1st 3 names remain the same. I will give u a sample dataset. Let me know if there is any easy way of doing this.? Pardon the displaced column labels. Original dataset. ?? a b c d??????????? ? e 1 2 3 4 5 10 9 8 7 6 Dataset for name change Original? New e ee b bb a aa c cc d dd I want my final dataset to be like this: aa bb cc dd ee 1 2 3 4 5 10 9 8 7 6 ?Could u tell me an optimal way to do it. My method is tedious and not good. Also, is there a way to import .xls without perl (windows)? Thanks for being patient. :)
Hi, Assuming that this is the case: dat1 <- read.table(text="a???? b???? c???? d?????? e 1???? 2???? 3???? 4???? 5 10???? 9???? 8???? 7???? 6",sep="",header=TRUE) Names1<- read.table(text="Original????? New?? e???? ee g??? gg a???? aa c???? cc f???? ff",sep="",header=TRUE,stringsAsFactors=FALSE) ? ?indx <- match(names(dat1),Names1[,1]) ?names(dat1)[names(dat1) %in% Names1[,1]] <- Names1[,2][indx[!is.na(indx)]] ?dat1 #? aa b cc d ee #1? 1 2? 3 4? 5 #2 10 9? 8 7? 6 A.K. On Friday, November 22, 2013 4:46 AM, Nitisha jha <nitisha999 at gmail.com> wrote: Hey! I got this one. :) For the match function, actually I just want the ones that are matching to be replaced. Rest should stay the same. How do I do that? When I tried your command, if there is no match, it writes var2 or something.?>>>On Fri, Nov 22, 2013 at 12:38 AM, arun <smartpink111 at yahoo.com> wrote: >>> >>> >>>> >>>>Hi, >>>>Try: >>>> >>>>dat1 <- read.table(text="a ??? b ??? c ??? d?????? e >>>> >>>>1 ??? 2 ??? 3 ??? 4 ??? 5 >>>>10 ??? 9 ??? 8 ??? 7 ??? 6",sep="",header=TRUE) >>>> >>>>Names1<- read.table(text="Original? ??? New??? >>>> >>>>e ??? ee >>>>b ??? bb??? >>>>a ??? aa >>>>c ??? cc >>>>d ??? dd",sep="",header=TRUE,stringsAsFactors=FALSE) >>>> >>>>It is better to dput() your dataset.? For example: >>>>?dput(Names1) >>>>structure(list(Original = c("e", "b", "a", "c", "d"), New = c("ee", >>>>"bb", "aa", "cc", "dd")), .Names = c("Original", "New"), class = "data.frame", row.names = c(NA, >>>>-5L)) >>>> >>>> >>>>?names(dat1) <- Names1[,2][match(names(dat1), Names1[,1])] ## >>>>?dat1 >>>>#? aa bb cc dd ee >>>>#1? 1? 2? 3? 4? 5 >>>>#2 10? 9? 8? 7? 6 >>>>A.K. >>>> >>>> >>>> >>>> >>>> >>>> >>>>On Thursday, November 21, 2013 1:45 PM, Nitisha jha <nitisha999 at gmail.com> wrote: >>>> >>>>Hi, >>>> >>>>Thanks. I used as.character() and got the right strings. >>>> >>>>Btw, I have lots of handicaps regarding R. >>>> >>>>I have to rename the columns(I have 22 columns here). I have the new names along with the original names in another dataset. Right now, I am going hardcoding all the? 19 name changes(tedious and not optimum). 1st 3 names remain the same. I will give u a sample dataset. Let me know if there is any easy way of doing this.? Pardon the displaced column labels. >>>> >>>> >>>> >>>>Original dataset. >>>> >>>> >>>> >>>>?? a b c d??????????? ? >>>>e >>>>1 2 3 4 5 >>>>10 9 8 7 6 >>>> >>>> >>>> >>>> >>>> >>>>Dataset for name change >>>> >>>> >>>>Original? New >>>> >>>> >>>> >>>>e ee >>>> >>>> >>>> >>>>b bb >>>> >>>> >>>> >>>>a aa >>>> >>>> >>>> >>>>c cc >>>> >>>> >>>> >>>>d dd >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>>I want my final dataset to be like this: >>>> >>>>aa bb cc dd ee >>>>1 2 3 4 5 >>>>10 9 8 7 6 >>>> >>>> >>>> >>>>?Could u tell me an optimal way to do it. My method is tedious and not good. >>>> >>>>Also, is there a way to import .xls without perl (windows)? >>>> >>>> >>>>Thanks for being patient. :) >>>> >>> >> >