日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

snmp 获取mac add table_R语言学习笔记(1)数据获取与数据重塑的第一部分

發(fā)布時間:2024/4/19 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 snmp 获取mac add table_R语言学习笔记(1)数据获取与数据重塑的第一部分 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

話接上回,怎么做這個圖

1.數(shù)據(jù)獲取:

https://github.com/CSSEGISandData/COVID-19這里只需要 "\csse_covid_19_data\csse_covid_19_daily_reports" 目錄下的每天的csv數(shù)據(jù),其實也還有個文件夾是時間序列數(shù)據(jù),但是疫情大熔爐US的數(shù)據(jù)沒有分各個州,做熱力圖的話那么一大片土地上只有一個紅點點沒啥意思,還是用daily_reports里的數(shù)據(jù)搞吧。GitHub可以下zip解壓,可以clone,這都不是重點。2.數(shù)據(jù)重塑:這一堆csv大概分三部分,第一部分是3月1日之前的,表格里面沒有經緯度信息,也就是今天要搞的數(shù)據(jù);第二部分是3月1日到3月21日,有經緯度信息,US數(shù)據(jù)是按州統(tǒng)計;第三部分是3月21日之后的,US數(shù)據(jù)是按城市統(tǒng)計。試驗了一下,按城市統(tǒng)計的熱力圖紅點紅不起來,就全部整理成按州統(tǒng)計的數(shù)據(jù),大概生成下面這樣的數(shù)據(jù)框。 Province.State Country.Region Confirmed Deaths Recovered Latitude Longitude1 South Korea 3150 16 27 36 1282 Italy 1128 29 46 43 123 Iran 593 43 123 32 534 Japan 241 5 32 36 1385 Singapore 102 0 72 1.2833 103.83336 France 100 2 12 47 2
  • R的數(shù)據(jù)類型
常規(guī)的就是字符串、數(shù)字、邏輯,這些到哪都一樣。然后是向量,矩陣,數(shù)組,數(shù)據(jù)框,列表,具體參見 https://www.runoob.com/r/r-array.html 。前三個本質都是數(shù)組,只是維度不同,元素必須是同一類型。數(shù)據(jù)框就是把多個向量按列組合在一起,每一列就是一個向量,元素也要同一類型(也就是上圖里的顏色)。
  • csv->data frame
先安裝大名鼎鼎的dplyr。這里直接安裝全家桶“tidyverse”完事。install.package('tidyverse')library(tidyverse)

?

data_dir?'~/COVID-19/csse_covid_19_data/csse_covid_19_daily_reports/'filesfor?(f?in?files[1:39]){ #3月1日前的數(shù)據(jù)??df?"UTF-8-BOM")???df #數(shù)據(jù)重塑函數(shù),下面寫??write.csv(df,paste0('~/covid_data/',f))?#數(shù)據(jù)框寫到另一個目錄下}

得到df是下面這樣的:需要完善數(shù)據(jù)

1.添加兩列經緯度

2.把NA改成0

Province.State Country.Region Last.Update Confirmed Deaths Recovered1 Anhui M*inland China 1/22/2020 17:00 1 NA NA.14 Hubei M*inland China 1/22/2020 17:00 444 17 28.21 Macau Macau 1/22/2020 17:00 1 NA NA.32 Washington US 1/22/2020 17:00 1 NA NA.37 Thailand 1/22/2020 17:00 2 NA NA38 South Korea 1/22/2020 17:00 1 NA NA

經緯度數(shù)據(jù)在https://github.com/CSSEGISandData/COVID-19有,在csse_covid_19_data/UID_ISO_FIPS_LookUp_Table.csv 里面

getLonlat function(){ geo_file'~/COVID-19/csse_covid_19_data/UID_ISO_FIPS_LookUp_Table.csv' lonlat% filter(Admin2=='') %>% select(Province_State,Country_Region,Lat,Long_) %>% filter(!is.na(Lat)) names(lonlat)'Province.State', return(lonlat)}lonlat#經緯度

%>% 是管道,相當于linux里面的"|",把前面一句的結果傳遞給后面一句。上面的3-6行翻譯成人話就是:

“讀取csv的數(shù)據(jù),再把Admin2是空的行篩選出來,再選擇Province_State, Country_Region, Lat,Long_這四列,再把Lat是空的行刪掉,最后得到的數(shù)據(jù)賦值給lonlat”

有些函數(shù)不支持管道,但是tidyverse包里的好像都可以。

lonlat里的是“China”,這里先把M*inland刪掉:

df$Country.Region$Country.Region,'M*inland ','')

按省份國家合并,然后時間列刪掉:

df% ??left_join(lonlat,by=c('Province.State','Country.Region')) %>%??select(-Last.Update)

關于join:

把NA改成0:

df[c('Confirmed',"Deaths","Recovered")][is.na(df[c('Confirmed',"Deaths","Recovered")])]<-0


得到df如下:

經緯度里還有NA,怎么辦?原始數(shù)據(jù)跟geo表格對不上,改好一個發(fā)現(xiàn)后面又對不上……算了,用map api搜吧

Province.State Country.Region Confirmed Deaths Recovered Lat Long_1 Anhui China 1 0 0 31.82570 117.22642 Beijing China 14 0 0 40.18240 116.41423 Chongqing China 6 0 0 30.05720 107.8740.14??????????Hubei??????????China???????444?????17????????28?30.97560??112.2707.36 Japan 2 0 0 36.20482 138.252937 Thailand 2 0 0 15.87003 100.992538 South Korea 1 0 0 NA NA

因為地名是英文,高德百度的api不好用,這里使用了某個不存在的api,具體方法不予顯示:

get_geocode_google function(a){ #install.package('httr') #library(httr) url'https://maps.googleapis.com/maps/api/geocode/json' key'==手動滑稽==' reslist(key=key,address=a),use_proxy( json if (json$status=="ZERO_RESULTS"){return(list(lat=0,lng=0))} return(json$results[[1]]$geometry$location)}add_geo2df function(df){ for (i in 1:nrow(df)){ geo if (!exists('df_geo')){ df_geo }else{ df_geo } } return(df %>% select(-Lat,-Long_,-address) %>% bind_cols(df_geo))}df1% filter(is.na(Lat)) %>% #經緯度為空的行 mutate(address=paste(Province.State,Country.Region)) #生成新的列,用于查詢apidf1#使用api查詢經緯度df% filter(!is.na(Lat)) %>% #經緯度不為空的行 bind_rows(df1) %>% #與前面的行合并 filter(!(Lat==0&Long_==0)) #再把經緯度為0的行刪掉

獲得完整數(shù)據(jù)如下:

Province.State Country.Region Confirmed Deaths Recovered Lat Long_1 Anhui China 1 0 0 31.82570 117.22642 Beijing China 14 0 0 40.18240 116.41423 Chongqing China 6 0 0 30.05720 107.8740.13 Hubei China 444 17 28 30.97560 112.2707.38 South Korea 1 0 0 35.90776 127.7669

下面是目前為止的R文件,跑完應該就能獲得39個csv文件

library(tidyverse)library(httr)data_dir '~/COVID-19/csse_covid_19_daily_reports/'fileslist.files(data_dir,full.names = F)getLonlat function(){ geo_file'~/COVID-19/csse_covid_19_data/UID_ISO_FIPS_LookUp_Table.csv' lonlat% filter(Admin2=='') %>% select(Province_State,Country_Region,Lat,Long_) %>% filter(!is.na(Lat)) names(lonlat)'Province.State',??lonlat'Diamond?Princess?cruise?ship',Country.Region= Lat=35.4437,Long_=139.6380)?#手動添加鉆石公主號坐標 return(lonlat)}lonlat#經緯度dfget_geocode_googlefunction(a){ url'https://maps.googleapis.com/maps/api/geocode/json'??key'YOUR_GOOGLE_MAPS_API_KEY'? reslist(key=key,address=a),use_proxy( json if (json$status=="ZERO_RESULTS"){return(list(lat=0,lng=0))} return(json$results[[1]]$geometry$location)}#把經緯度加入數(shù)據(jù)框add_geo2dffunction(df){ for (i in 1:nrow(df)){ geo if (!exists('df_geo')){ df_geo }else{ df_geo } } return(df %>% select(-Lat,-Long_,-address) %>% bind_cols(df_geo))}dataReshape function(df){??df$Country.Region'M*inland?', df% left_join(lonlat,by=c('Province.State','Country.Region')) %>% select(-Last.Update) df[c('Confirmed',"Deaths","Recovered")][is.na(df[c('Confirmed',"Deaths","Recovered")])]<-0 df1% filter(is.na(Lat)) %>% mutate(address=paste(Province.State,Country.Region)) df1 df% filter(!is.na(Lat)) %>% bind_rows(df1) %>% filter(!(Lat==0&Long_==0))}for (f in files[1:39]){ df"UTF-8-BOM") df write.csv(df,paste0(getwd(),'/covid/',f))}

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的snmp 获取mac add table_R语言学习笔记(1)数据获取与数据重塑的第一部分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。