Skip to content

Insert row in specific location between data frames

13 messages · pigpigmeow, Rui Barradas, Peter Ehlers +3 more

#
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.
#
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:
#
On 2012-06-29 10:01, pigpigmeow wrote:
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
#
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 <glorykwok at hotmail.com>
To: r-help at r-project.org
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.

______________________________________________
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.
#
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
#
On Jun 29, 2012, at 2:04 PM, Peter Ehlers wrote:

            
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.
#
On 2012-06-29 11:29, David Winsemius wrote:
??
Works fine for me on Windows Vista, bot 32/64-bit.

Also works for one-column dataframes.

Peter
#
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 <dcarlson at tamu.edu>
To: 'Peter Ehlers' <ehlers at ucalgary.ca>; 'pigpigmeow' <glorykwok at hotmail.com>
Cc: r-help at r-project.org
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
______________________________________________
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.
#
On Jun 29, 2012, at 2:48 PM, Peter Ehlers wrote:

            
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
David Winsemius, MD
West Hartford, CT
#
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
#
On 2012-06-29 11:08, arun wrote:
But don't do this with more than one column in your dataframe.

Peter Ehlers
#
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 <ehlers at ucalgary.ca>
To: arun <smartpink111 at yahoo.com>
Cc: pigpigmeow <glorykwok at hotmail.com>; R help <r-help at r-project.org>
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:
But don't do this with more than one column in your dataframe.

Peter Ehlers
1 day later
#
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
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.