[R] Insert row in specific location between data frames

Rui Barradas ruipbarradas at sapo.pt
Mon Jul 2 09:27:51 CEST 2012


Hello,

When I've asked you to dput() your datasets, I meant all of the output 
of dput(), for us to copy it and paste in an R session. It is the 
easiest way of recreating exact copies of the objects.

Like this, with those "................" it's unusable.

Now, as far as I can see, you have a data.frame called groupA with 25 
rows and a vector of 24 elements. after including an NA in 11th position 
the vector length becomes 25. This part was already answered to.

Then you want to put that vector as a column of groupA. You do NOT need 
'with', this will do:

groupA$predict_SO2 <- predict_SO2_a


Then you want to merge this resulting data.frame with another 
data.frame, groupB, right? But merge returns a data.frame with 0 rows.

What went wrong? The common columns combined don't have the same values.

Why not? Because column 'Date' is a factor, not a date. The labels, 
i.e., the dates values, might be equal but the factors, how they are 
coded, are not.

Use the following.


x <- with(groupA, levels(Date)[Date])
x <- as.Date(x, format="%d/%m/%Y")
groupA$Date <- x

And the same for groupB. Only then try to merge them.

And next time paste the output of dput(), ALL of it.

Hope this helps,

Rui Barradas

Em 02-07-2012 05:39, pigpigmeow escreveu:
> First, I have "predict_SO2_a" which is contained 24 data. I want to insert
> "NA" in 11th row. Then, "predict_SO2_a" becomes 25 data.
> After insert the row, I want to use "with" function to combine the
> data.frame
> />groupA$predict_SO2<-with(groupA, predict_SO2_a).
> /
> />dput(predict_SO2_a)
> c(39.7932308121176, 30.25257753285, 32.4675835451901, 31.9415094289634,
> 27.9083195877186, 11.5941369504695, 9.36812510512633, 12.3190926962636,
> .......................
> 14.9134904913096, 33.8462160039482, 16.6586503422101, 11.0312717522444,
> 22.3102431270508, 15.1408236735915, 10.6875527887638, 11.3294850253127,
> 13.9037966719703, 28.6603710864312)
>
>> dput(groupA)
> structure(list(Date = structure(c(15L, 21L, 23L, 20L, 9L, 10L,
> 2L, 11L, 22L, 6L, 7L, 16L, 17L, 24L, 26L, 12L, 18L, 19L, 14L,
> 8L, 25L, 3L, 4L, 5L, 13L), .Label = c("", "1/9/2001", "10/9/2010",
> "11/9/2010", "12/9/2010", "14/9/2002", "15/9/2002", "15/9/2009",
> "19/9/1999", "2/9/2000", "2/9/2001", "2/9/2008", "21/9/2010",
> "24/9/2008", "3/9/1997", "3/9/2003", "3/9/2005", "3/9/2008",
> "5/9/2008", "6/9/1998", "7/9/1997", "7/9/2001", "8/9/1997", "8/9/2006",
> "8/9/2010", "9/9/2006"), class = "factor"), pressure = c(-8.110989011,
> -5.910989011, -3.510989011, -4.732967033, -5.737362637, -7.607692308,
> -9.675824176, -9.075824176, -5.575824176, -6.169230769, -8.169230769,
> -9.207692308, -9.197802198, -4.884615385, -3.684615385, -3.132967033,
> -3.332967033, -3.232967033, -9.532967033, -8.537362637, -6.869230769,
> -6.869230769, -3.869230769, -2.069230769, -5.369230769), maxtemp =
> c(2.056043956,
> 0.756043956, 1.556043956, 2.216483516, 1.995604396, 2.346153846,
> 1.97032967, 0.17032967, 1.57032967, 0.747252747, -0.352747253,
> 0.672527473, 1.985714286, 1.452747253, 0.352747253, 1.568131868,
> 3.068131868, 1.368131868, 0.168131868, 1.987912088, 5.187912088,
> 3.987912088, -0.812087912, 1.587912088, -1.112087912), avetemp =
> c(2.540659341,
> 0.440659341, 1.340659341, 1.287912088, 2.278021978, 2.2, 1.962637363,
> 0.962637363, 1.562637363, 1.482417582, 0.682417582, 1.089010989,
> 2.103296703, 1.989010989, 0.589010989, 2.087912088, 2.287912088,
> 1.787912088, 1.287912088, 1.330769231, 5.237362637, 3.43736263
> .............................
> ratio = c(1.53920929073912,
> ...................
> 2.08020364225369, 2.5845449621267, 4.68646633242131, 0.93343593089835,
> 1.18698605729367, 1.19133323040343, 1.9902213063946, 2.09049362040035
> )), .Names = c("Date", "pressure", "maxtemp", "avetemp", "mintemp",
> "RH", "solar", "windspeed", "transport", "angle", "rainfall",
> "RSP", "Ozone", "NO2", "NOX", "SO2", "CO", "newRSP", "newOzone",
> "newNO2", "newNOX", "newSO2", "predict_RSP", "predict_NO2", "predict_NOX",
> "ratio"), row.names = c(NA, 25L), class = "data.frame")/
>
> Finally, I want to groupA combine with groupB, groupB contains ......
>
> /dput(groupB)
> structure(list(Date = structure(c(1L, 16L, 20L, 27L, 32L, 34L,
> 35L, 7L, 11L, 21L, 30L, 17L, 8L, 2L, 28L, 3L, 18L, 22L, 24L,
> 29L, 31L, 23L, 25L, 4L, 26L, 12L, 13L, 15L, 19L, 5L, 6L, 33L,
> 9L, 10L, 14L), .Label = c("1/9/1997", "1/9/2004", "1/9/2006",
> "1/9/2008", "10/11/2009", "11/11/2009", "11/9/1999", "12/10/2003",
> "13/9/2010", "17/9/2010", "18/9/1999", "18/9/2008", "18/9/2009",
> "18/9/2010", "19/9/2009", "2/9/1997", "2/9/2002", "2/9/2006",
> "20/9/2009", "26/11/1997", "3/10/2000", "3/9/2006", "3/9/2007",
> "4/9/2006", "4/9/2007", "4/9/2008", "5/9/1998", "5/9/2004", "5/9/2006",
> "6/9/2001", "6/9/2006", "7/9/1998", "7/9/2010", "8/9/1998", "9/9/1998"
> ), class = "factor"), pressure = c(-8.310989011, -8.710989011,
> -1.710989011, -4.732967033, -2.932967033, -2.732967033, -5.432967033,
> -6.637362637, -7.237362637, -1.707692308, -6.475824176, -3.869230769,
> -3.507692308, -8.098901099, -10.6989011, -7.184615385, ....................
>   ratio = c(1.94158182541644, 2.12248234979731,
> 1.87302150800523, 2.61289013672199, 2.97067043253228, 2.85053235533923,
> 2.51886435993509, 1.87829582620638, ........................
> 2.9380496638884, 1.40686764084479,
> 0.858666346292962)), .Names = c("Date", "pressure", "maxtemp",
> "avetemp", "mintemp", "RH", "solar", "windspeed", "transport",
> "angle", "rainfall", "RSP", "Ozone", "NO2", "NOX", "SO2", "CO",
> "newRSP", "newOzone", "newNO2", "newNOX", "newSO2", "predict_RSP",
> "predict_NO2", "predict_NOX", "predict_SO2", "ratio"), row.names = c(NA,
> -35L), class = "data.frame")/
>
>
> I used "merge" function to combine groupA and groupB. totally contains 60
> data
> mab<-merge(groupA,groupB)
>
> however, it shows...
> />mab
>   [1] Date        pressure    maxtemp     avetemp     mintemp     RH
> solar       windspeed   transport   angle       rainfall    RSP
> Ozone
> [14] NO2         NOX         SO2         CO          newRSP      newOzone
> newNO2      newNOX      newSO2      predict_RSP predict_NO2 predict_NOX
> predict_SO2
> [27] ratio
> <0 rows> (or 0-length row.names)/
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Insert-row-in-specific-location-between-data-frames-tp4634905p4635071.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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