An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110724/bbd15f75/attachment.pl>
How to merge a matrix and a dataframe with different types of columns
2 messages · Marco, PIKAL Petr
1 day later
Hi
[R] How to merge a matrix and a dataframe with different types of
columns
Hi, I hope someone can help here. I have been scratching my head for many hours, trying to find out how to merge two datasets that were created by different means. The initial miniMarket contains dates without discontinuities (weekends). This matrix was created using seq and has a character nature right now.
miniMarket
[1] "2011-07-22" "2011-07-21" "2011-07-20" "2011-07-19" "2011-07-18" "2011-07-17" "2011-07-16" "2011-07-15" [9] "2011-07-14" "2011-07-13" "2011-07-12" "2011-07-11" "2011-07-10" "2011-07-09" "2011-07-08" "2011-07-07" [17] "2011-07-06" "2011-07-05" "2011-07-04" "2011-07-03"
Hm. You are not telling the whole story typeof(seq(as.Date(Sys.time())-100, as.Date(Sys.time()),10) ) [1] "double" str(seq(as.Date(Sys.time())-100, as.Date(Sys.time()),10) ) Date[1:11], format: "2011-04-16" "2011-04-26" "2011-05-06" "2011-05-16" ...
typeof(miniMarket)
[1] "character"
Anyway, you can change it to date by as.Date function.
The miniTechies dataframe was the resulted of a sql query and had a
double
nature (typeof), despite of being presented as a date
<snip>
When I try to merge them by the dates, I fail
mergedM <- as.matrix(merge(miniMarket,miniTechies[,2],by=1,all=TRUE))
Why as.matrix? this transforms it to matrix which needs to have the same type of data so R tries it best to transform everithing to suit this condition. what do you mean by=1 By my opinion you need Convert both sets to data frame and for further operation to name the columns you want to merge by with the same name. Transform values in both common columns to the same type of data, in your case most probably by as.Date do mergedM <- merge(df1, df2, all=T) Regards Petr BTW I needed to look at merge help page and the same should do you.
Warning message: In `[<-.factor`(`*tmp*`, ri, value = c(20L, 19L, 18L, 17L, 16L, : invalid factor level, NAs generated
mergedM
x
[1,] "2011-07-03"
[2,] "2011-07-04"
[3,] "2011-07-05"
[4,] "2011-07-06"
[5,] "2011-07-07"
[6,] "2011-07-08"
[7,] "2011-07-09"
[8,] "2011-07-10"
[9,] "2011-07-11"
[10,] "2011-07-12"
[11,] "2011-07-13"
[12,] "2011-07-14"
[13,] "2011-07-15"
[14,] "2011-07-16"
[15,] "2011-07-17"
[16,] "2011-07-18"
[17,] "2011-07-19"
[18,] "2011-07-20"
[19,] "2011-07-21"
[20,] "2011-07-22"
[21,] NA
[22,] NA
[23,] NA
[24,] NA
[25,] NA
I didn't try to reproduce the means to create these datasets because I
don't
really understand how the Date is being represented in the miniTechies dataframe. Could anyone provide me some hint on how to merge these datasets. What I want is a dataset with all dates and not only dates for which there is
data
on the miniTechies dataframe. Thanks, Marco [[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.