Merging different columns.

Hi, I'm, very beginning user of RStudio, so my commands may look a bit chaotic.
I have to merge different columns from different files: I want to merge column "dane_mc$id" with "polska$wmo_id". I want to:
-count the average from "dane_mc$t2_mean_mon",
-merge the column with average with "polska$wmo_id",
-organize the names of stations alphabetically (dane_mc$station_names).

My code:

install.packages('dplyr')

#Cwiczenia z meteorologii i klimatologii
#ustawienie katalogu roboczego

setwd('C:/Users/marlew33/Desktop/klimat')
getwd()
install.packages('dplyr')
install.packages('RCurl')
install.packages('XML')
install.packages('remotes')

#wczytujemy i instalujemy potrzebne biblioteki

library(dplyr)
library(RCurl)
library(XML)
library(remotes)
install_github("bczernecki/climate")
'force=TRUE'
library(climate)
imgw_hydro_abbrev

#pobieramy metadane o stacjach w Polsce

install.packages("maps")
polska <- stations_ogimet(country='Poland', add_map=T)
head(polska)

#w głowie się kręci się

view(polska)
tail(polska)
str(polska)
dim(polska)
polska$station_names
nazwy_stacji <- polska$station_names
polska$wmo_id
numery_stacji <- polska$wmo_id
c(1:10)
c(1966:2023)
dane_mc <- meteo_imgw(interval="monthly", rank="synop", year=c(1966:2022), coords=F)
dane_mc
view(dane_mc)

colnames(dane_mc)
str(dane_mc)
unique(dane_mc$station)
#sprawdzwielkosc
object.size(dane_mc)
saveRDS(dane_mc, 'dane_mc_polska.rds')
install.packages('pryr')
library(pryr)
dir()

mmp <- readRDS('dane_mc_polska.rds')
unique(mmp$rr_montly)
unique(mmp$yy)
colnames(mmp)
boxplot(mmp$tmax_abs)
head(mmp)
hist(mmp$tmax_abs)
unique(mmp$station)
unique(mmp[,c("station", 'X','Y')])
head(mmp)
polska[1,5]
polska[(1:2),4]
polska[1:200,4]
polska[1:31,c(1,5)]
library(dplyr)

str(polska)
polska$station_names
str(dane_mc_polska)
całete <- dane_mc_polska$t2m_mean_mon
całete
head(mmp$rr_montly)
meteo_imgw

dane_mc <- meteo_imgw(interval = 'monthly', rank='synop', year = c(1966:2023), coords = T)
head(dane_mc)

mmp$station[mmp$station=="KOŁOBRZEG-DŹWIRZYNO"] <- "KOŁOBRZEG"
mmp$station[mmp$station=="WROCŁAW-STRACHOWICE"] <- "WROCŁAW"
mmp$station[mmp$station=="WARSZAWA-OKĘCIE"] <- "WARSZAWA"
mmp$station[mmp$station=="ŁÓDŹ-LUBLINEK"] <- "ŁÓDŹ"

mmp$x[mmp$station == "BYDGOSZCZ"] <- 18.00697320455025
mmp$y[mmp$station == "BYDGOSZCZ"] <- 53.106748886337996

mmp$X[mmp$station == "HALA GĄSIENICOWA"] <- 20.005760788001748
mmp$Y[mmp$station == "HALA GĄSIENICOWA"] <- 49.24411556595931

Hi, welcome to the forum

I have gone through your code, removed a lot of your data checking code which makes sense when you are first writing the code but just adds clutter now. I have also moved all the library() calls to the top of the script to be tidy. :slight_smile:

You are doing something I do not understand, well several things, but the first one ins you are loading dane_mc twice, firstly with coords = F and then with coords = T did you mean to do this? If so why?

dane_mc <- meteo_imgw(interval="monthly",   rank="synop", year=c(1966:2022), coords=F)
# later
dane_mc <- meteo_imgw(interval = 'monthly', rank='synop', year = c(1966:2023), coords = T)

This command will not work as there is no dane_mc_polska. You have saved dane_mc as dane_mc_polska.rds but you are reading it back in as mmp.

całete <- dane_mc_polska$t2m_mean_mon 

There is a t2m_mean_mon in dane_mc and in mmp`.

This will not work because mmp$x does not exist.

mmp$X[mmp$station == "HALA GĄSIENICOWA"] <- 20.005760788001748

I think you may want something like this:

mmp$x[mmp$station == "BYDGOSZCZ"] <- 18.00697320455025 

which creates a new variable x.
I am afraid that this is as far as I have gotten. My modified file is below


library(dplyr)
library(RCurl)
library(XML)
library(remotes)
library(climate)
library(maps)
# library(pryr)  # Why?

polska <- stations_ogimet(country='Poland', add_map=T)
dane_mc <- meteo_imgw(interval="monthly",       # This is repeated with "coords = T" later. 
    rank="synop", year=c(1966:2022), coords=F)  # Is this intentional?

#w głowie się kręci się  :)

nazwy_stacji <- polska$station_names
numery_stacji <- polska$wmo_id

saveRDS(dane_mc, 'dane_mc_polska.rds')  # Why?
mmp <- readRDS('dane_mc_polska.rds')
unique(mmp$rr_monthly)  # corrected typo  "monthly"
boxplot(mmp$tmax_abs)
hist(mmp$tmax_abs)

całete <- dane_mc_polska$t2m_mean_mon  # dane_mc_polska does not exist 
                                       # There is a
całete

dane_mc <- meteo_imgw(interval = 'monthly', rank='synop', year = c(1966:2023), coords = T) # Repeat, see note above.
head(dane_mc)


# This seems fine. 
mmp$station[mmp$station=="KOŁOBRZEG-DŹWIRZYNO"] <- "KOŁOBRZEG"
mmp$station[mmp$station=="WROCŁAW-STRACHOWICE"] <- "WROCŁAW"
mmp$station[mmp$station=="WARSZAWA-OKĘCIE"] <- "WARSZAWA"
mmp$station[mmp$station=="ŁÓDŹ-LUBLINEK"] <- "ŁÓDŹ"


mmp$x[mmp$station == "BYDGOSZCZ"] <- 18.00697320455025  # mmp$x does not exist
mmp$y[mmp$station == "BYDGOSZCZ"] <- 53.106748886337996 # mmp$y does not exist

mmp$X[mmp$station == "HALA GĄSIENICOWA"] <- 20.005760788001748 # mmp$X does not exist
mmp$Y[mmp$station == "HALA GĄSIENICOWA"] <- 49.24411556595931  # mmp$Y does not exist

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.