0

I have a folder containing many shapefiles that are somehow related but contain different numbers of attributes in the corresponding database (dbf-File). I managed to load them all into R using

files<-list.files(path=".",pattern=".shp$")
files<-sub(".shp","",files)
for(i in files){
  assign(i,readOGR(".",i))
}

Now the problem is that the corresponding information are different, sometimes containing a column called "legend" and sometimes not.

> str(A@data)
'data.frame':   1704 obs. of  7 variables:
 $ NRKART    : int  6 1 7 15 7 15 1 7 999 999 ...
 $ SCHRAFFUR : Factor w/ 2 levels "A","L": NA NA NA NA NA NA NA NA NA NA ...
 $ TKLE_NR   : int  151806 151801 151807 151815 151807 151815 151801 151807 151800 151800 ...
 $ Symbol    : Factor w/ 86 levels "1","10","100",..: 61 1 62 22 62 22 1 62 54 54 ...
 $ BGL       : Factor w/ 11 levels "0.0","1.1","1.2",..: 2 2 2 3 2 3 2 2 1 1 ...
 $ Shape_Area: num  4123982 20460030 436214 3904785 364182 ...
 $ Shape_Len : num  65111 43803 3152 19753 3174 ...

> str(B@data)
    'data.frame':   2705 obs. of  8 variables:
     $ NRKART    : int  22 14 48 42 27 14 14 1 999 48 ...
     $ SCHRAFFUR : Factor w/ 1 level "A": NA NA NA NA NA NA NA NA NA NA ...
     $ TKLE_NR   : int  471822 471814 471848 471842 471827 471814 471814 471801 471800 471848 ...
     $ BGL       : Factor w/ 15 levels "0.0","10.1","11.1",..: 11 11 13 12 12 11 11 6 1 13 ...
     $ SYMBOL_NR : Factor w/ 80 levels "107","161","176",..: 1 24 18 56 67 24 24 36 22 18 ...
     $ Legende   : Factor w/ 84 levels "00 Gewässerflächen",..: 23 15 49 43 28 15 15 2 1 49 ...
     $ Shape_Area: num  2300557 568922 2512166 1044624 13708524 ...
     $ Shape_Len : num  13251 3298 7387 5235 40118 ...

My prefered way of merging the two would be to use

library(taRifx.geo)
New<-rbind(A,B,fix.duplicated.IDs=TRUE)

How do I import or rename each attributed dataframe so it contains all the columns of shape B. The name of column Symbol (A) needs to be changed in SYMBOL_NR ,too

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
user2386786
  • 725
  • 9
  • 25

2 Answers2

0

If I understand correctly, you want to append the files together, not merge them (you are trying to have the rows of A followed by the rows of B, not trying to combine information from A and B into a single row).

For that, I'd suggest bind_rows from dplyr (you can use rename to fix the column while you are at it). It should match up column names as needed, and fill in the NA's where appropriate.

New <-
  bind_rows(
    A@data %>%
      rename(SYMBOL_NR = Symbol)
    , B@data
  )
Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • Your suggestion works for data.frames but I work with shapefiles containing both a dataframe and geographical data. Therefore your solution does not quite work. – user2386786 Oct 28 '16 at 14:41
  • I don't work with shapefiles, so I apologize for the confusion. I didn't realize that `rbind` had a method for handling the additional portions of the shape file. Alternatively, if the geographical data is identical in the two sets, could you assign the `@data` accessor with the `New` data.frame? Or, do a similar `bind_rows` for the other parts of the shapefile? – Mark Peterson Oct 28 '16 at 14:51
0

Following this

, and assuming that the comment by @Mark Peterson is doing what you need with the data, this could do what you ask ( if I got the question right):

newpolys <- rbind(A, B, makeUniqueIDs = TRUE) 
newdata  <- bind_rows(A@data %>%
                   rename(SYMBOL_NR = Symbol)
                  , B@data
newpolys@data = newdata
Community
  • 1
  • 1
lbusett
  • 5,801
  • 2
  • 24
  • 47