2

I am sure its an easy thing to do, but I did not find anything in the search function.

My question is: I have 3 different dataframes and want to select 1 special row out of each dataframe and put these rows together in a new dataframe. How do I do that?

Here is an example:

>df1
  Station     Date    Value
1 Station 1 20000608   5.0
2 Station 1 20000609  17.5
3 Station 2 20000610  30.0

>df2
  Station     Date    Value
1 Station 7 20010608     7
2 Station 5 20020609    14
3 Station 1 20060610    21

> df3
  Station     Date    Value
1 Station 5 20050608     2
2 Station 3 20020609     5
3 Station 2 20010610     8

###################
#Code
x1= c("Station 1", "Station 1", "Station 2")
x2= c("20000608", "20000609", "20000610")
x3= seq(5, 30, length=3)

df1 = data.frame(Station=x1, Date=x2, Value=x3) 

x1= c("Station 7", "Station 5", "Station 1")
x2= c("20010608", "20020609", "20060610")
x3= seq(7, 21, length=3)

df2 = data.frame(Station=x1, Date=x2, Value=x3) 

x1= c("Station 5", "Station 3", "Station 2")
x2= c("20050608", "20020609", "20010610")
x3= seq(2, 8, length=3)

df3 = data.frame(Station=x1, Date=x2, Value=x3) 

I want to extact the 2nd row out of df1, the 1st row out of df2 and the 1st row out of df3. All 3 rows should be added in a new dataframe. It should look like this in the end:

>dfnew
  Station     Date    Value
2 Station 1 20000609  17.5
1 Station 7 20010608     7
1 Station 5 20050608     2

I know of course

df1[2,], df2[1,], df3[1,] #or
df[ c(2,), (1,), (1,)]

But that only works for the same dataframe. How can I do that with multiple dataframes?

Essi
  • 761
  • 3
  • 12
  • 22

1 Answers1

4

You can do it by selecting rows you want from all the data frames and then bind them together using:

dfnew <- rbind(df1[2, ], df2[1, ], df3[1, ])

It'll give you the desired output:

   Station   Date      Value
2  Station 1 20000609  17.5
1  Station 7 20010608   7.0
11 Station 5 20050608   2.0
sm925
  • 2,648
  • 1
  • 16
  • 28