Dear R helpers I have one data as given below. date value1 value2 value3 30-Nov-2010 100 40 61 25-Nov-2010 108 31 88 14-Sep-2010 11 180 56 I want the following output date name amount 30-Nov-2010 value1 100 30-Nov-2010 value2 40 30-Nov-2010 value3 61 25-Nov-2010 value1 108 25-Nov-2010 value2 31 25-Nov-2010 value3 88 14-Sep-2010 value1 11 14-Sep-2010 value2 180 14-Sep-2010 value3 56 I have presented here a small part of large data. I tried to convert the data into matrix, then transpose etc. but things are not working for me. Please guide Thanking in advance Amy Milano [[alternative HTML version deleted]]
Hello, Amy, take a look at ?reshape In your case, I think, reshape( yourdatafame, varying = c( "value1", "value2", "value3"), v.name = "amount", times = c( "value1", "value2", "value3"), timevar = "name", direction = "long") should work. Hth -- Gerrit On Fri, 17 Dec 2010, Amy Milano wrote:> Dear R helpers > > I have one data as given below. > > date???????????????????? value1????????? value2???????????? value3 > 30-Nov-2010?????????? 100???????????????? 40???????????????? 61 > 25-Nov-2010?????????? 108???????????????? 31???????????????? 88 > 14-Sep-2010??????????? 11???????????????? 180?????????????? 56 > > I want the following output > > date???????????????? name ?? ?? amount > 30-Nov-2010????? value1 ? ?? ? 100 > 30-Nov-2010????? value2???????? 40 > 30-Nov-2010????? value3???????? 61 > 25-Nov-2010????? value1? ???? 108 > 25-Nov-2010????? value2 ? ????? 31 > 25-Nov-2010 ? ?? value3??? ? ?? 88? > 14-Sep-2010????? value1??????? 11 > 14-Sep-2010????? value2 ???? 180 > 14-Sep-2010 ? ?? value3 ? ?? 56 > ...
Hi, You can use the reshape package and the melt function : melt(data, id="date") Alain On 17-Dec-10 10:40, Amy Milano wrote:> Dear R helpers > > I have one data as given below. > > date value1 value2 value3 > 30-Nov-2010 100 40 61 > 25-Nov-2010 108 31 88 > 14-Sep-2010 11 180 56 > > I want the following output > > date name amount > 30-Nov-2010 value1 100 > 30-Nov-2010 value2 40 > 30-Nov-2010 value3 61 > 25-Nov-2010 value1 108 > 25-Nov-2010 value2 31 > 25-Nov-2010 value3 88 > 14-Sep-2010 value1 11 > 14-Sep-2010 value2 180 > 14-Sep-2010 value3 56 > > > I have presented here a small part of large data. I tried to convert the data into matrix, then transpose etc. but things are not working for me. Please guide > > Thanking in advance > > Amy Milano > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Alain Guillet Statistician and Computer Scientist SMCS - IMMAQ - Universit? catholique de Louvain uclouvain.be/smcs Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50
Hi use melt from reshape package> test<-data.frame(a=letters[1:3], v1=1:3, v2=5:7, v3=100:102) > testa v1 v2 v3 1 a 1 5 100 2 b 2 6 101 3 c 3 7 102> melt(test)Using a as id variables a variable value 1 a v1 1 2 b v1 2 3 c v1 3 4 a v2 5 5 b v2 6 6 c v2 7 7 a v3 100 8 b v3 101 9 c v3 102 Regards Petr r-help-bounces at r-project.org napsal dne 17.12.2010 10:40:14:> Dear R helpers > > I have one data as given below. > > date value1 value2 value3 > 30-Nov-2010 100 40 61 > 25-Nov-2010 108 31 88 > 14-Sep-2010 11 180 56 > > I want the following output > > date name amount > 30-Nov-2010 value1 100 > 30-Nov-2010 value2 40 > 30-Nov-2010 value3 61 > 25-Nov-2010 value1 108 > 25-Nov-2010 value2 31 > 25-Nov-2010 value3 88 > 14-Sep-2010 value1 11 > 14-Sep-2010 value2 180 > 14-Sep-2010 value3 56 > > > I have presented here a small part of large data. I tried to convert thedata> into matrix, then transpose etc. but things are not working for me.Please guide> > Thanking in advance > > Amy Milano > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guideR-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
El 17/12/10 10:40, Amy Milano escribió:> Dear R helpers > > I have one data as given below. > > date value1 value2 value3 > 30-Nov-2010 100 40 61 > 25-Nov-2010 108 31 88 > 14-Sep-2010 11 180 56 > > I want the following output > > date name amount > 30-Nov-2010 value1 100 > 30-Nov-2010 value2 40 > 30-Nov-2010 value3 61 > 25-Nov-2010 value1 108 > 25-Nov-2010 value2 31 > 25-Nov-2010 value3 88 > 14-Sep-2010 value1 11 > 14-Sep-2010 value2 180 > 14-Sep-2010 value3 56 > > > I have presented here a small part of large data. I tried to convert the data into matrix, then transpose etc. but things are not working for me. Please guide > > Thanking in advance > > Amy Milano > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help@r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.First, you must load the following libraries: library( plyr ) library( reshape ) Next, define the following function: Transform <- function(d.f){aux <- dim(d.f);files <- aux[1];columnes <- aux[2];aux2<-as.factor(rep(1:files,columnes));return(cbind(aux2,melt(d.f)))} So, if you put your data.frame into object aux: aux <- data.frame(c(100,108,11),c(40,31,180),c(61,88,56)) colnames(aux) <- c("value1","value2","value3") rownames(aux) <- c("30 Nov 2010","25 Nov 2010","14 Sep 2010") you can transform it in the following way: aux2 <- Transform(aux) aux2[,1] <- as.factor(rep(rownames(aux),3)) Finally, you obtain what you want. Arnau. [[alternative HTML version deleted]]