学习 R 笔记

rm(x) #删除
print()
show()
ls() #show current data and values in project
rm(list = ls()) # can delete all values

install.packages(“gdata”) #安装包裹
library(gdata) #load package
require(gdata) #try to load package

#表格例子1
bsp4 <- structure(list(Name = c(“Hans”, “Caro”, “Lars”, “Ines”, “Samira”, “Peter”, “Sarah”), Geschlecht = structure(c(2L, 3L, 1L, 3L, 3L,2L, 3L), .Label = c(“intersexuell”, “maennlich”, “weiblich”), class = “factor”),Lieblingsfarbe = structure(c(3L, 1L, 2L, 4L, 2L, 3L, 1L), .Label = c(“blau”,”gelb”, “gruen”, “schwarz”), class = “factor”), Einkommen = c(1233,800,2400,4000,899,1100,1900)), .Names = c(“Name”,”Geschlecht”, “Lieblingsfarbe”, “Einkommen”), row.names = c(NA,7L), class = “data.frame”)

#一种Data vs. Values 的解释
dat <- data.frame(a=1:10, b=letters[1:10])
#And this would move it to the Values section:
dat <- unclass(dat)

#另一种读取shapefile方式!数据被存为”Data”
library(‘sf’)
#Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
deutmap_data = read_sf(‘/R/ak_maps/DEU_adm_shp/DEU_adm1.shp’)

#shapefile地图从http://www.gadm.org/country下载!数据被存为”Values”
#注意!.dbf .prj .shp .shx要放在一起,他们是一起的,不能分开。
library(rgdal)
deutmap=readOGR(‘/R/ak_maps/DEU_adm_shp/DEU_adm2.shp’)
deutmap<-readOGR(‘/R/ak_maps/DEU_adm_shp/DEU_adm2.shp’)
plot(deutmap) #无颜色的地图
library(RColorBrewer)
colr=rainbow(length(levels(deutmap$NAME_1)))
spplot(deutmap,”NAME_1″,colr.regions=colr,main=”German Regions”, colorkey=TRUE,lwd=1,colr=”red”)

#读取地图另一种方法
library(rgeos)
library(maptools)
d_map=readShapeSpatial(“/R/ak_maps/DEU_adm_shp/DEU_adm1.shp”)
plot(d_map) # VERIFY IT LOADED PROPERLY
d_map=st_read(“/R/ak_maps/DEU_adm_shp/DEU_adm1.shp”)

#读取文件 并且改变编码
text <- readLines(file.choose(),encoding = “UTF-8”)

#读取excel文件.xlsx
library(readxl)
auslaender <- read_excel(“C:/R/ak_rawdata/auslaender.xlsx”)
View(auslaender)
str(auslaender) # allows you to see the classes of the variables (all numeric)

## create progress bar
total=length(deutmap_data)
order <- vector()
progbar=txtProgressBar(min = 0, max = total, style = 3)
for (i in 1:total){
order[i] <- agrep(deutmap_data[i], auslaender$Bundersländer,
max.distance = 0.1)[1]
setTxtProgressBar(progbar, i) # update progress bar
warning(order)
}

#example for grepl
data <- data.frame(animal = sample(c(“cat”,”dog”,”bird”, ‘doggy’,’kittycat’), 50, replace = T)) ifelse(grepl(“dog|cat”, data$animal), “keep”, “discard”)

#做词云方法 https://www.r-bloggers.com/using-wordcloud-on-search-terms-phrases/
> require(wordcloud)
> require(RColorBrewer)
> datain <- read.csv(“./tcr deso dsl bk1.csv”, colClasses=c(“character”, “numeric”),encoding = “UTF-8″) > pal2 <- brewer.pal(8,”Dark2”) > png(“wordcloud.png”, width=1000,height=1000)
> wordcloud(datain$term,datain$freq, scale=c(6,.5),min.freq=1, max.words=Inf, random.order=FALSE, rot.per=.15, colors=pal2)
> dev.off()
#可借鉴的来源https://rpubs.com/brandonkopp/creating-word-clouds-in-r

发表回复

您的电子邮箱地址不会被公开。