arrange data
Hi, You could also do this with: dat2<- dat1 ?dat2[]<-lapply(dat1,function(x) c(x[!is.na(x)],x[is.na(x)])) row.names(res1)<- row.names(dat2) ?identical(dat2,res1) #[1] TRUE For your new question: test<- data.frame(medrw1) ?row.names(test) ? [1] "1"?? "2"?? "3"?? "4"?? "5"?? "6"?? "7"?? "8"?? "9"?? "10"? "11"? "12" ?[13] "13"? "14"? "15"? "16"? "17"? "18"? "19"? "20"? "21"? "22"? "23"? "24" ?[25] "25"? "26"? "27"? "28"? "29"? "30"? "31"? "32"? "33"? "34"? "35"? "36" ?[37] "37"? "38"? "39"? "40"? "41"? "42"? "43"? "44"? "45"? "46"? "47"? "48" ?[49] "49"? "50"? "51"? "52"? "53"? "54"? "55"? "56"? "57"? "58"? "59"? "60" ?[61] "61"? "62"? "63"? "64"? "65"? "66"? "67"? "68"? "69"? "70"? "71"? "72" ?[73] "73"? "74"? "75"? "76"? "77"? "78"? "79"? "80"? "81"? "82"? "83"? "84" ?[85] "85"? "86"? "87"? "88"? "89"? "90"? "91"? "92"? "93"? "94"? "95"? "96" ?[97] "97"? "98"? "99"? "100" "101" "102" "103" "104" "105" "106" "107" "108" [109] "109" "110" "111" "112" "113" "114" "115" "116" "117" "118" "119" "120" [121] "121" "122" "123" "124" "125" "126" "127" "128" "129" "130" "131" "132" [133] "133" "134" "135" "136" "137" "138" "139" "140" ?test$newCol<- row.names(test) ?test1<-test[,c(2:1)] ?head(test1) #? newCol medrw1 #1????? 1? 1.499 #2????? 2? 1.209 #3????? 3? 1.400 #4????? 4? 1.590 #5????? 5? 1.463 #6????? 6? 1.771 A.K.
From: catalin roibu <catalinroibu at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Saturday, April 6, 2013 2:27 PM
Subject: Re: [R] arrange data
To: arun <smartpink111 at yahoo.com>
Sent: Saturday, April 6, 2013 2:27 PM
Subject: Re: [R] arrange data
It is not for this case. I want to create a growth mean by row and compare with another serie mean. medrw1 <- apply(res,1,median,na.rm=TRUE) test<-cbind(medrw1)? in this case I want to create a new column at first position?which contain this seq().??This column I want to set as row.names. Thank you very much! On 6 April 2013 21:24, arun <smartpink111 at yahoo.com> wrote: > > >________________________________ >From: catalin roibu <catalinroibu at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Saturday, April 6, 2013 1:43 PM >Subject: Re: [R] arrange data > > >I want a column with seq(1,nrow(res),by=1) and set this column as row.names. > > >Thank you very much! > > > >On 6 April 2013 20:41, arun <smartpink111 at yahoo.com> wrote: > > >>>________________________________ >>?From: catalin roibu <catalinroibu at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Saturday, April 6, 2013 1:20 PM >> >>Subject: Re: [R] arrange data >> >> >>It is perfect! Thank you very much! A last question if you don't mind. How can add in the first position a new column ( from 1 to max row?length) ?which I use as rowname?? >> >> >> >>> >>>On 6 April 2013 20:06, arun <smartpink111 at yahoo.com> wrote: >>> >>> >>>> >>>> >>>>?lst1<-lapply(dat1,function(x) x[!is.na(x)]) >>>>?res<-as.data.frame(sapply(lst1,function(x) c(x,rep(NA,max(sapply(lst1,length))-length(x)) ))) >>>>?head(res) >>>> >>>>#???? 1B??? 2B??? 4B??? 1A??? 2A??? 4A??? 5B??? 5A? C31A? C31B? C34A? C34B? C35A >>>>#1 2.518 2.357 1.499 3.647 1.890 2.249 2.896 2.175 0.452 1.177 0.344 0.612 1.722 >>>>#2 2.796 2.759 1.032 3.763 1.182 2.291 2.417 2.597 1.033 0.831 0.351 0.509 1.102 >>>>#3 4.012 2.878 1.126 3.236 1.431 2.635 2.952 2.483 0.679 0.484 0.704 0.461 1.400 >>>>#4 3.611 2.094 1.043 3.032 1.835 3.019 1.590 2.269 0.901 0.576 1.008 0.879 2.222 >>>>#5 2.970 1.371 1.486 1.817 1.884 2.557 1.768 1.463 0.466 0.378 0.708 0.862 1.840 >>>>#6 4.687 1.042 1.252 3.495 2.540 2.592 1.179 1.860 0.608 0.314 0.721 0.624 1.927 >>>> >>>>#?? C35B? C37A? C37B? C36A? C36B? C32A? C32B? C33A? C33B? C39B >>>>#1 2.482 0.996 0.898 0.179 1.148 2.637 2.917 1.440 1.487 1.291 >>>>#2 1.061 1.243 1.078 1.167 1.209 2.770 2.325 2.006 1.886 0.655 >>>>#3 1.350 0.999 0.992 1.629 1.551 1.996 2.444 1.261 1.343 0.790 >>>>#4 1.952 1.227 1.447 1.067 2.252 2.220 1.635 1.085 1.367 0.633 >>>>#5 2.343 1.049 1.285 1.299 1.182 2.976 2.106 1.388 2.253 0.630 >>>>#6 2.104 1.771 1.204 1.540 1.203 2.847 2.200 2.563 2.348 0.493 >>>>nrow(res) >>>>#[1] 140 >>>>If you wanted to have the same number of rows as dat1: >>>>nrow(dat1) >>>>#[1] 145 >>>>new1<-as.data.frame(matrix(NA,ncol=23,nrow=5)) >>>>?colnames(new1)<- colnames(res) >>>>?res1<- rbind(res,new1) >>>>?nrow(res1) >>>>#[1] 145 >>>> >>>> >>>> >>>>A.K. >>>> >>>> >>>> >> >