Insert row in specific location between data frames

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Insert row in specific location between data frames

pigpigmeow
Hi everyone! I have a simple question.

my data is
 predict_SO2_a
1       39.793231
2       30.252578
3       32.467584
4       31.941509
5       27.908320
6       11.594137
7        9.368125
8       12.319093
9       11.558811
10       7.937192
11      11.211306
12      12.400342
13      12.393146
14      13.256160
15      10.709600
16       9.966334
17      28.850652
18      10.024405



I want to insert row which is "NA" in 10th row

that is ..
 predict_SO2_a
1       39.793231
2       30.252578
3       32.467584
4       31.941509
5       27.908320
6       11.594137
7        9.368125
8       12.319093
9       11.558811
10     NA
....
and it becomes 19 rows in this data.


however, I can't do this. my scipt is following
topdata <- predict_SO2_a[1:10,]
bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a),]
nadata <- data.frame(NA)
d1 <- rbind(topdata,nadata)
d2 <- rbind(d1, bottomdata)

what is my problem?!

Thank in advance!


Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

Rui Barradas
Hello,

If you print 'topdata' and 'bottomdata' you'll see that they are not
data.frames, they're vectors. This is because you are extracting one
column only, which happens to be the only one.
Solution: use c().

topdata <- predict_SO2_a[1:10, ]
bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a), ]
nadata <- NA
d1 <- c(topdata, nadata)
d2 <- data.frame(predict_SO2_a=c(d1, bottomdata))
d2


Hope this helps,

Rui Barradas

Em 29-06-2012 18:01, pigpigmeow escreveu:

> Hi everyone! I have a simple question.
>
> my data is
>   predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10       7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16       9.966334
> 17      28.850652
> 18      10.024405
>
>
>
> I want to insert row which is "NA" in 10th row
>
> that is ..
>   predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10     NA
> ....
> and it becomes 19 rows in this data.
>
>
> however, I can't do this. my scipt is following
> topdata <- predict_SO2_a[1:10,]
> bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a),]
> nadata <- data.frame(NA)
> d1 <- rbind(topdata,nadata)
> d2 <- rbind(d1, bottomdata)
>
> what is my problem?!
>
> Thank in advance!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Insert-row-in-specific-location-between-data-frames-tp4634905.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

Peter Ehlers
In reply to this post by pigpigmeow

On 2012-06-29 10:01, pigpigmeow wrote:

> Hi everyone! I have a simple question.
>
> my data is
>   predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10       7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16       9.966334
> 17      28.850652
> 18      10.024405
>
>
>
> I want to insert row which is "NA" in 10th row

Try this:

   d <- data.frame(x = 101:118, y = rnorm(18))
   d2 <- data.frame(
              rbind(head(d, 9), NA, tail(d, -9)),
              row.names = NULL)

Peter Ehlers

>
> that is ..
>   predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10     NA
> ....
> and it becomes 19 rows in this data.
>
>
> however, I can't do this. my scipt is following
> topdata<- predict_SO2_a[1:10,]
> bottomdata<- predict_SO2_a[11:nrow(predict_SO2_a),]
> nadata<- data.frame(NA)
> d1<- rbind(topdata,nadata)
> d2<- rbind(d1, bottomdata)
>
> what is my problem?!
>
> Thank in advance!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Insert-row-in-specific-location-between-data-frames-tp4634905.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

arun kirshna
In reply to this post by pigpigmeow
Hi,

You can try this:

dat1<-read.table(text="
predict_SO2_a
1      39.793231
2      30.252578
3      32.467584
4      31.941509
5      27.908320
6      11.594137
7        9.368125
8      12.319093
9      11.558811
10      7.937192
11      11.211306
12      12.400342
13      12.393146
14      13.256160
15      10.709600
16      9.966334
17      28.850652
18      10.024405
",sep="",header=TRUE)

 dat2<-dat1[10,]
 dat2<-NA
dat3<-data.frame(predict_SO2_a=c(dat1[1:9,],dat2,dat1[10:18,]))

A.K.




----- Original Message -----
From: pigpigmeow <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, June 29, 2012 1:01 PM
Subject: [R] Insert row in specific location between data frames

Hi everyone! I have a simple question.

my data is
predict_SO2_a
1       39.793231
2       30.252578
3       32.467584
4       31.941509
5       27.908320
6       11.594137
7        9.368125
8       12.319093
9       11.558811
10       7.937192
11      11.211306
12      12.400342
13      12.393146
14      13.256160
15      10.709600
16       9.966334
17      28.850652
18      10.024405



I want to insert row which is "NA" in 10th row

that is ..
predict_SO2_a
1       39.793231
2       30.252578
3       32.467584
4       31.941509
5       27.908320
6       11.594137
7        9.368125
8       12.319093
9       11.558811
10     NA
....
and it becomes 19 rows in this data.


however, I can't do this. my scipt is following
topdata <- predict_SO2_a[1:10,]
bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a),]
nadata <- data.frame(NA)
d1 <- rbind(topdata,nadata)
d2 <- rbind(d1, bottomdata)

what is my problem?!

Thank in advance!




--
View this message in context: http://r.789695.n4.nabble.com/Insert-row-in-specific-location-between-data-frames-tp4634905.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

David Carlson
In reply to this post by Peter Ehlers
You are having the problem because you have a one column data frame. When
you extract that column, R converts it to a vector. You can insert the NA
into the vector and then convert it to a data.frame or you can prevent R
from converting the data.frame to a vector and insert the row using a
slightly modified version of your code:

# Prevent conversion to a vector with drop=FALSE
topdata <- predict_SO2_a[1:10, , drop=FALSE]
bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a), , drop=FALSE]

# Assign a column name in nadata so rbind works properly
nadata <- data.frame(predict_SO2_a=NA)

# skip d1 step and merge everything at once. rbind() gives you a
# data frame, but the row.names are messed up, this command fixes that
d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Peter Ehlers
> Sent: Friday, June 29, 2012 1:04 PM
> To: pigpigmeow
> Cc: [hidden email]
> Subject: Re: [R] Insert row in specific location between data frames
>
>
> On 2012-06-29 10:01, pigpigmeow wrote:
> > Hi everyone! I have a simple question.
> >
> > my data is
> >   predict_SO2_a
> > 1       39.793231
> > 2       30.252578
> > 3       32.467584
> > 4       31.941509
> > 5       27.908320
> > 6       11.594137
> > 7        9.368125
> > 8       12.319093
> > 9       11.558811
> > 10       7.937192
> > 11      11.211306
> > 12      12.400342
> > 13      12.393146
> > 14      13.256160
> > 15      10.709600
> > 16       9.966334
> > 17      28.850652
> > 18      10.024405
> >
> >
> >
> > I want to insert row which is "NA" in 10th row
>
> Try this:
>
>    d <- data.frame(x = 101:118, y = rnorm(18))
>    d2 <- data.frame(
>               rbind(head(d, 9), NA, tail(d, -9)),
>               row.names = NULL)
>
> Peter Ehlers
>
> >
> > that is ..
> >   predict_SO2_a
> > 1       39.793231
> > 2       30.252578
> > 3       32.467584
> > 4       31.941509
> > 5       27.908320
> > 6       11.594137
> > 7        9.368125
> > 8       12.319093
> > 9       11.558811
> > 10     NA
> > ....
> > and it becomes 19 rows in this data.
> >
> >
> > however, I can't do this. my scipt is following
> > topdata<- predict_SO2_a[1:10,]
> > bottomdata<- predict_SO2_a[11:nrow(predict_SO2_a),]
> > nadata<- data.frame(NA)
> > d1<- rbind(topdata,nadata)
> > d2<- rbind(d1, bottomdata)
> >
> > what is my problem?!
> >
> > Thank in advance!
> >
> >
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Insert-
> row-in-specific-location-between-data-frames-tp4634905.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

David Winsemius
In reply to this post by Peter Ehlers

On Jun 29, 2012, at 2:04 PM, Peter Ehlers wrote:

>
> On 2012-06-29 10:01, pigpigmeow wrote:
>> Hi everyone! I have a simple question.
>>
>> my data is
>>  predict_SO2_a
>> 1       39.793231
>> 2       30.252578
>> 3       32.467584
>> 4       31.941509
>> 5       27.908320
>> 6       11.594137
>> 7        9.368125
>> 8       12.319093
>> 9       11.558811
>> 10       7.937192
>> 11      11.211306
>> 12      12.400342
>> 13      12.393146
>> 14      13.256160
>> 15      10.709600
>> 16       9.966334
>> 17      28.850652
>> 18      10.024405
>>
>>
>>
>> I want to insert row which is "NA" in 10th row
>
> Try this:
>
>  d <- data.frame(x = 101:118, y = rnorm(18))
>  d2 <- data.frame(
>             rbind(head(d, 9), NA, tail(d, -9)),
>             row.names = NULL)

That threw an error for me.
Error in do.call(f, cal0) : second argument must be a list

  What about this?

d[ c( 1:9, NA, 10:NROW(d) ), ]

I do not like this behavior of "[" but I suppose it is useful sometimes.

--

David Winsemius, MD
West Hartford, CT


>
> Peter Ehlers
>
>>
>> that is ..
>>  predict_SO2_a
>> 1       39.793231
>> 2       30.252578
>> 3       32.467584
>> 4       31.941509
>> 5       27.908320
>> 6       11.594137
>> 7        9.368125
>> 8       12.319093
>> 9       11.558811
>> 10     NA
>> ....
>> and it becomes 19 rows in this data.
>>
>>
>> however, I can't do this. my scipt is following
>> topdata<- predict_SO2_a[1:10,]
>> bottomdata<- predict_SO2_a[11:nrow(predict_SO2_a),]
>> nadata<- data.frame(NA)
>> d1<- rbind(topdata,nadata)
>> d2<- rbind(d1, bottomdata)
>>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

Peter Ehlers
On 2012-06-29 11:29, David Winsemius wrote:

>
> On Jun 29, 2012, at 2:04 PM, Peter Ehlers wrote:
>
>>
>> On 2012-06-29 10:01, pigpigmeow wrote:
>>> Hi everyone! I have a simple question.
>>>
>>> my data is
>>>   predict_SO2_a
>>> 1       39.793231
>>> 2       30.252578
>>> 3       32.467584
>>> 4       31.941509
>>> 5       27.908320
>>> 6       11.594137
>>> 7        9.368125
>>> 8       12.319093
>>> 9       11.558811
>>> 10       7.937192
>>> 11      11.211306
>>> 12      12.400342
>>> 13      12.393146
>>> 14      13.256160
>>> 15      10.709600
>>> 16       9.966334
>>> 17      28.850652
>>> 18      10.024405
>>>
>>>
>>>
>>> I want to insert row which is "NA" in 10th row
>>
>> Try this:
>>
>>   d<- data.frame(x = 101:118, y = rnorm(18))
>>   d2<- data.frame(
>>              rbind(head(d, 9), NA, tail(d, -9)),
>>              row.names = NULL)
>
> That threw an error for me.
> Error in do.call(f, cal0) : second argument must be a list

??
Works fine for me on Windows Vista, bot 32/64-bit.

Also works for one-column dataframes.

Peter

>
>    What about this?
>
> d[ c( 1:9, NA, 10:NROW(d) ), ]
>
> I do not like this behavior of "[" but I suppose it is useful sometimes.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

arun kirshna
In reply to this post by David Carlson
Hi David,

I am getting error messages with the code.
dat1<-read.table(text="
predict_SO2_a
1      39.793231
2      30.252578
3      32.467584
4      31.941509
5      27.908320
6      11.594137
7        9.368125
8      12.319093
9      11.558811
10      7.937192
11      11.211306
12      12.400342
13      12.393146
14      13.256160
15      10.709600
16      9.966334
17      28.850652
18      10.024405
",sep="",header=TRUE)


topdata <- dat1[1:10, , drop=FALSE]
bottomdata <- dat1[11:nrow(dat1), , drop=FALSE]
nadata <- data.frame(dat1=NA)
d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)


Error in match.names(clabs, names(xi)) :
  names do not match previous names


A.K.




----- Original Message -----
From: David L Carlson <[hidden email]>
To: 'Peter Ehlers' <[hidden email]>; 'pigpigmeow' <[hidden email]>
Cc: [hidden email]
Sent: Friday, June 29, 2012 2:28 PM
Subject: Re: [R] Insert row in specific location between data frames

You are having the problem because you have a one column data frame. When
you extract that column, R converts it to a vector. You can insert the NA
into the vector and then convert it to a data.frame or you can prevent R
from converting the data.frame to a vector and insert the row using a
slightly modified version of your code:

# Prevent conversion to a vector with drop=FALSE
topdata <- predict_SO2_a[1:10, , drop=FALSE]
bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a), , drop=FALSE]

# Assign a column name in nadata so rbind works properly
nadata <- data.frame(predict_SO2_a=NA)

# skip d1 step and merge everything at once. rbind() gives you a
# data frame, but the row.names are messed up, this command fixes that
d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Peter Ehlers
> Sent: Friday, June 29, 2012 1:04 PM
> To: pigpigmeow
> Cc: [hidden email]
> Subject: Re: [R] Insert row in specific location between data frames
>
>
> On 2012-06-29 10:01, pigpigmeow wrote:
> > Hi everyone! I have a simple question.
> >
> > my data is
> >   predict_SO2_a
> > 1       39.793231
> > 2       30.252578
> > 3       32.467584
> > 4       31.941509
> > 5       27.908320
> > 6       11.594137
> > 7        9.368125
> > 8       12.319093
> > 9       11.558811
> > 10       7.937192
> > 11      11.211306
> > 12      12.400342
> > 13      12.393146
> > 14      13.256160
> > 15      10.709600
> > 16       9.966334
> > 17      28.850652
> > 18      10.024405
> >
> >
> >
> > I want to insert row which is "NA" in 10th row
>
> Try this:
>
>    d <- data.frame(x = 101:118, y = rnorm(18))
>    d2 <- data.frame(
>               rbind(head(d, 9), NA, tail(d, -9)),
>               row.names = NULL)
>
> Peter Ehlers
>
> >
> > that is ..
> >   predict_SO2_a
> > 1       39.793231
> > 2       30.252578
> > 3       32.467584
> > 4       31.941509
> > 5       27.908320
> > 6       11.594137
> > 7        9.368125
> > 8       12.319093
> > 9       11.558811
> > 10     NA
> > ....
> > and it becomes 19 rows in this data.
> >
> >
> > however, I can't do this. my scipt is following
> > topdata<- predict_SO2_a[1:10,]
> > bottomdata<- predict_SO2_a[11:nrow(predict_SO2_a),]
> > nadata<- data.frame(NA)
> > d1<- rbind(topdata,nadata)
> > d2<- rbind(d1, bottomdata)
> >
> > what is my problem?!
> >
> > Thank in advance!
> >
> >
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Insert-
> row-in-specific-location-between-data-frames-tp4634905.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

David Winsemius
In reply to this post by Peter Ehlers

On Jun 29, 2012, at 2:48 PM, Peter Ehlers wrote:

> On 2012-06-29 11:29, David Winsemius wrote:
>>
>> On Jun 29, 2012, at 2:04 PM, Peter Ehlers wrote:
>>
>>>
>>> On 2012-06-29 10:01, pigpigmeow wrote:
>>>> Hi everyone! I have a simple question.
>>>>
>>>> my data is
>>>>  predict_SO2_a
>>>> 1       39.793231
>>>> 2       30.252578
>>>> 3       32.467584
>>>> 4       31.941509
>>>> 5       27.908320
>>>> 6       11.594137
>>>> 7        9.368125
>>>> 8       12.319093
>>>> 9       11.558811
>>>> 10       7.937192
>>>> 11      11.211306
>>>> 12      12.400342
>>>> 13      12.393146
>>>> 14      13.256160
>>>> 15      10.709600
>>>> 16       9.966334
>>>> 17      28.850652
>>>> 18      10.024405
>>>>
>>>>
>>>>
>>>> I want to insert row which is "NA" in 10th row
>>>
>>> Try this:
>>>
>>>  d<- data.frame(x = 101:118, y = rnorm(18))
>>>  d2<- data.frame(
>>>             rbind(head(d, 9), NA, tail(d, -9)),
>>>             row.names = NULL)
>>
>> That threw an error for me.
>> Error in do.call(f, cal0) : second argument must be a list
>
> ??
> Works fine for me on Windows Vista, bot 32/64-bit.

Seems to be the rbind call. MacOS / R 2.14.2
R version 2.14.2 (2012-02-29)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)


d2<- rbind(head(d, 9), NA, tail(d, -9))
Error in do.call(f, cal0) : second argument must be a list

>
> Also works for one-column dataframes.
>
> Peter
>
>>
>>   What about this?
>>
>> d[ c( 1:9, NA, 10:NROW(d) ), ]
>>
>> I do not like this behavior of "[" but I suppose it is useful  
>> sometimes.
>>
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

David Carlson
In reply to this post by arun kirshna
Here you want the name of the column, not the data frame:

nadata <- data.frame(predict_SO2_a=NA)

instead of

nadata <- data.frame(dat1=NA)

Then it will work.


-------
David

> -----Original Message-----
> From: arun [mailto:[hidden email]]
> Sent: Friday, June 29, 2012 2:01 PM
> To: [hidden email]
> Cc: R help
> Subject: Re: [R] Insert row in specific location between data frames
>
> Hi David,
>
> I am getting error messages with the code.
> dat1<-read.table(text="
> predict_SO2_a
> 1      39.793231
> 2      30.252578
> 3      32.467584
> 4      31.941509
> 5      27.908320
> 6      11.594137
> 7        9.368125
> 8      12.319093
> 9      11.558811
> 10      7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16      9.966334
> 17      28.850652
> 18      10.024405
> ",sep="",header=TRUE)
>
>
> topdata <- dat1[1:10, , drop=FALSE]
> bottomdata <- dat1[11:nrow(dat1), , drop=FALSE]
> nadata <- data.frame(dat1=NA)
> d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)
>
>
> Error in match.names(clabs, names(xi)) :
>   names do not match previous names
>
>
> A.K.
>
>
>
>
> ----- Original Message -----
> From: David L Carlson <[hidden email]>
> To: 'Peter Ehlers' <[hidden email]>; 'pigpigmeow'
> <[hidden email]>
> Cc: [hidden email]
> Sent: Friday, June 29, 2012 2:28 PM
> Subject: Re: [R] Insert row in specific location between data frames
>
> You are having the problem because you have a one column data frame.
> When
> you extract that column, R converts it to a vector. You can insert the
> NA
> into the vector and then convert it to a data.frame or you can prevent
> R
> from converting the data.frame to a vector and insert the row using a
> slightly modified version of your code:
>
> # Prevent conversion to a vector with drop=FALSE
> topdata <- predict_SO2_a[1:10, , drop=FALSE]
> bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a), , drop=FALSE]
>
> # Assign a column name in nadata so rbind works properly
> nadata <- data.frame(predict_SO2_a=NA)
>
> # skip d1 step and merge everything at once. rbind() gives you a
> # data frame, but the row.names are messed up, this command fixes that
> d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)
>
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
>
> > -----Original Message-----
> > From: [hidden email] [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Peter Ehlers
> > Sent: Friday, June 29, 2012 1:04 PM
> > To: pigpigmeow
> > Cc: [hidden email]
> > Subject: Re: [R] Insert row in specific location between data frames
> >
> >
> > On 2012-06-29 10:01, pigpigmeow wrote:
> > > Hi everyone! I have a simple question.
> > >
> > > my data is
> > >   predict_SO2_a
> > > 1       39.793231
> > > 2       30.252578
> > > 3       32.467584
> > > 4       31.941509
> > > 5       27.908320
> > > 6       11.594137
> > > 7        9.368125
> > > 8       12.319093
> > > 9       11.558811
> > > 10       7.937192
> > > 11      11.211306
> > > 12      12.400342
> > > 13      12.393146
> > > 14      13.256160
> > > 15      10.709600
> > > 16       9.966334
> > > 17      28.850652
> > > 18      10.024405
> > >
> > >
> > >
> > > I want to insert row which is "NA" in 10th row
> >
> > Try this:
> >
> >    d <- data.frame(x = 101:118, y = rnorm(18))
> >    d2 <- data.frame(
> >               rbind(head(d, 9), NA, tail(d, -9)),
> >               row.names = NULL)
> >
> > Peter Ehlers
> >
> > >
> > > that is ..
> > >   predict_SO2_a
> > > 1       39.793231
> > > 2       30.252578
> > > 3       32.467584
> > > 4       31.941509
> > > 5       27.908320
> > > 6       11.594137
> > > 7        9.368125
> > > 8       12.319093
> > > 9       11.558811
> > > 10     NA
> > > ....
> > > and it becomes 19 rows in this data.
> > >
> > >
> > > however, I can't do this. my scipt is following
> > > topdata<- predict_SO2_a[1:10,]
> > > bottomdata<- predict_SO2_a[11:nrow(predict_SO2_a),]
> > > nadata<- data.frame(NA)
> > > d1<- rbind(topdata,nadata)
> > > d2<- rbind(d1, bottomdata)
> > >
> > > what is my problem?!
> > >
> > > Thank in advance!
> > >
> > >
> > >
> > >
> > > --
> > > View this message in context: http://r.789695.n4.nabble.com/Insert-
> > row-in-specific-location-between-data-frames-tp4634905.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > >
> > > ______________________________________________
> > > [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

Peter Ehlers
In reply to this post by arun kirshna
On 2012-06-29 11:08, arun wrote:

> Hi,
>
> You can try this:
>
> dat1<-read.table(text="
> predict_SO2_a
> 1      39.793231
> 2      30.252578
> 3      32.467584
> 4      31.941509
> 5      27.908320
> 6      11.594137
> 7        9.368125
> 8      12.319093
> 9      11.558811
> 10      7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16      9.966334
> 17      28.850652
> 18      10.024405
> ",sep="",header=TRUE)
>
>   dat2<-dat1[10,]
>   dat2<-NA
> dat3<-data.frame(predict_SO2_a=c(dat1[1:9,],dat2,dat1[10:18,]))
>
> A.K.

But don't do this with more than one column in your dataframe.

Peter Ehlers

>
>
>
>
> ----- Original Message -----
> From: pigpigmeow <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Friday, June 29, 2012 1:01 PM
> Subject: [R] Insert row in specific location between data frames
>
> Hi everyone! I have a simple question.
>
> my data is
> predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10       7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16       9.966334
> 17      28.850652
> 18      10.024405
>
>
>
> I want to insert row which is "NA" in 10th row
>
> that is ..
> predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10     NA
> ....
> and it becomes 19 rows in this data.
>
>
> however, I can't do this. my scipt is following
> topdata <- predict_SO2_a[1:10,]
> bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a),]
> nadata <- data.frame(NA)
> d1 <- rbind(topdata,nadata)
> d2 <- rbind(d1, bottomdata)
>
> what is my problem?!
>
> Thank in advance!
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

arun kirshna
Hi Peter,

With more than one columns,
#Suppose, I add one more column to the exisiting dat1


dat2<-data.frame(dat1,predict_b=rnorm(18,15))
dat3<-dat2[10,1]
 dat3<-NA
dat4<-data.frame(predict_SO2_a=c(dat2[1:9,1],dat3,dat2[10:18,1]),predict_b=c(dat2[1:9,2],dat3,dat2[10:18,2]))
  predict_SO2_a predict_b
1      39.793231  15.55819
2      30.252578  14.12883
3      32.467584  15.43390
4      31.941509  15.52307
5      27.908320  16.25137
6      11.594137  12.84646
7       9.368125  14.95992
8      12.319093  16.50440
9      11.558811  15.41039
10            NA        NA
11      7.937192  15.71090
12     11.211306  15.06633
13     12.400342  14.97070
14     12.393146  17.07543
15     13.256160  15.75680
16     10.709600  16.26108
17      9.966334  13.52237
18     28.850652  14.37278
19     10.024405  14.51609


I guess, the code gets uglier with more columns.

A.K.




----- Original Message -----
From: Peter Ehlers <[hidden email]>
To: arun <[hidden email]>
Cc: pigpigmeow <[hidden email]>; R help <[hidden email]>
Sent: Friday, June 29, 2012 5:19 PM
Subject: Re: [R] Insert row in specific location between data frames

On 2012-06-29 11:08, arun wrote:

> Hi,
>
> You can try this:
>
> dat1<-read.table(text="
> predict_SO2_a
> 1      39.793231
> 2      30.252578
> 3      32.467584
> 4      31.941509
> 5      27.908320
> 6      11.594137
> 7        9.368125
> 8      12.319093
> 9      11.558811
> 10      7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16      9.966334
> 17      28.850652
> 18      10.024405
> ",sep="",header=TRUE)
>
>   dat2<-dat1[10,]
>   dat2<-NA
> dat3<-data.frame(predict_SO2_a=c(dat1[1:9,],dat2,dat1[10:18,]))
>
> A.K.

But don't do this with more than one column in your dataframe.

Peter Ehlers

>
>
>
>
> ----- Original Message -----
> From: pigpigmeow <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Friday, June 29, 2012 1:01 PM
> Subject: [R] Insert row in specific location between data frames
>
> Hi everyone! I have a simple question.
>
> my data is
> predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10       7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16       9.966334
> 17      28.850652
> 18      10.024405
>
>
>
> I want to insert row which is "NA" in 10th row
>
> that is ..
> predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10     NA
> ....
> and it becomes 19 rows in this data.
>
>
> however, I can't do this. my scipt is following
> topdata <- predict_SO2_a[1:10,]
> bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a),]
> nadata <- data.frame(NA)
> d1 <- rbind(topdata,nadata)
> d2 <- rbind(d1, bottomdata)
>
> what is my problem?!
>
> Thank in advance!
>
>


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

pigpigmeow
In reply to this post by pigpigmeow
i'm not success to insert row in specific location and merge another file

I have a question and I feel confused about the usage of "rbind", "cbind" and "data.frame)

data 1:
> predict_SO2_a
    predict_SO2_a
1       39.793231
2       30.252578
3       32.467584
4       31.941509
5       27.908320
6       11.594137
7        9.368125
8       12.319093
9       11.558811
10       NA
11      11.211306
12      12.400342
13      12.393146
14      13.256160
15      10.709600
16       9.966334
17      28.850652
18      10.024405
data 2:
predict_SO2_b
 [1] 16.931975 18.286223 11.454404  6.256828  7.951174  5.364070  9.675208  7.328439  9.957388 14.603543 14.605044  8.681327  9.450510 10.628049 13.178781 11.757256
[17] 23.820308

when I used "merge" and combine data1 and data2, it showed the error
Error in rbind(deparse.level, ...) : replacement has length zero.
I found the display of these two data are different. I'm confused



Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

Rui Barradas
Hello,

Please use dpu() to post your datasets.
Example:

dput(predict_SO2_b)  # paste the output of this in a post


Hope this helps,

Rui Barradas


Em 01-07-2012 06:33, pigpigmeow escreveu:

> i'm not success to insert row in specific location and merge another file
>
> I have a question and I feel confused about the usage of "rbind", "cbind"
> and "data.frame)
>
> data 1:
>> predict_SO2_a
>      predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10       NA
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16       9.966334
> 17      28.850652
> 18      10.024405
> data 2:
> predict_SO2_b
>   [1] 16.931975 18.286223 11.454404  6.256828  7.951174  5.364070  9.675208
> 7.328439  9.957388 14.603543 14.605044  8.681327  9.450510 10.628049
> 13.178781 11.757256
> [17] 23.820308
>
> when I used "merge" and combine data1 and data2, it showed the error
> Error in rbind(deparse.level, ...) : replacement has length zero.
> I found the display of these two data are different. I'm confused
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Insert-row-in-specific-location-between-data-frames-tp4634905p4635020.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

David Winsemius
In reply to this post by pigpigmeow

On Jul 1, 2012, at 1:33 AM, pigpigmeow wrote:

> i'm not success to insert row in specific location and merge another  
> file
>
> I have a question and I feel confused about the usage of "rbind",  
> "cbind"
> and "data.frame)
>
> data 1:
>> predict_SO2_a
>    predict_SO2_a
> 1       39.793231
> 2       30.252578
> 3       32.467584
> 4       31.941509
> 5       27.908320
> 6       11.594137
> 7        9.368125
> 8       12.319093
> 9       11.558811
> 10       NA
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16       9.966334
> 17      28.850652
> 18      10.024405
> data 2:
> predict_SO2_b
> [1] 16.931975 18.286223 11.454404  6.256828  7.951174  5.364070  
> 9.675208
> 7.328439  9.957388 14.603543 14.605044  8.681327  9.450510 10.628049
> 13.178781 11.757256
> [17] 23.820308
>
> when I used "merge" and combine data1 and data2, it showed the error
> Error in rbind(deparse.level, ...) : replacement has length zero.
> I found the display of these two data are different. I'm confused

Yes, you are. The 'merge' operation requires that the two data.frames  
have some sort of identifier that labels the groups to be merged.

You should describe what you are attempting rather than showing failed  
efforts at doing it.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

pigpigmeow
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)


Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

Rui Barradas
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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Insert row in specific location between data frames

pigpigmeow
I have already follow your step, it still not work
when I merge groupA and groupB , the error message was shown
Error in rbind(deparse.level, ...) : replacement has length zero