How to reformat data frame to spatial data


#1

I want to plot spatial data, but don't know how to process the dataset.
For example, I have one shapefile, that can be opened with readOGR.
The other two datasets are about the attributes that I want to overlay the shapefile. Among them, one dataset (DF1) has precipitation for each grid cell, while the column name is the coordinate. The second dataset (DF2) has two rows, and many columns; the first row represent latitude and the second row represent longitude, and each column represent one grid cell corresponding to DF1. How to plot the precipitation data as a map, that has the shape as the shapefile? Thanks for your help.
DF1
lat1_lon1 lat1_lon2 lat1_lon3 ... lat2_lon1 lat2_lon2 ...
1.1 2.1 2,5 ... 2.7 1.8 ...

DF2
lat1 lat1 lat1 ... lat2 ...
lon1 lon2 lon3 ... lon1 ...

The above are examples of DF1 and DF2, note that DF1 has column names as the coordinates of each grid cell, while DF2 has no column names.


#2

It sounds like you're in for some data wrangling for your datasets before you get to plotting with shapefiles. The Tidy data vignette (see below) should help get you going:
https://tidyr.tidyverse.org/articles/tidy-data.html

There are some common patterns (e.g. Column headers are values, not variable names) that it sounds like you'll be able to follow for your data.

For getting started with mapping, you might want to take a look at the rspatial website:
http://www.rspatial.org/