码迷,mamicode.com
首页 > 编程语言 > 详细

R语言基本操作

时间:2020-03-19 22:07:41      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:plot   document   export   func   汇总   ack   ase   mem   tran   

is.na and is.element

is.na can use which, it finds specific rows, is.element can‘t, it is designed to find NA in a whole table

 

d[is.na(d)] <- 0
is.element(NA,temp_city$city_name)

 

delete certain rows

df <- df[-c(1,2),]#drop discrete rows
df <- df[-c(1:2),]#drop continous rows

 

  delete certain columns

trade_month_final <- trade_month_final[,-(2:3)]
shoppingmall_amap3 <- subset(shoppingmall_amap3,select=-c(origin_name))
shoppingmall_amap2 <- shoppingmall_amap2[,-c(‘origin_name‘)]
tmpdata <- data_set[ , !names(data_set) %in% c(“var1”, “var2”)]

 

 grepl,unique and gsub

content_int<-content_int[!grepl(‘TRUE‘,content_int$store_name)]
content_int<-unique(content_int,by=c(‘store_name‘))
content_int$store_name<-gsub[‘[(].*|[)].*‘,‘‘,content_int$store_name)]

 

 build a row which has the same number of rows in the table

table_temp1$month <- month

 

 remember, this only applies to numeric variables. When month is a data frame(like when it is calculated by aggregate function),we have to transfer it into numeric ones with as.numeric.

 

ave
city_statistic$group_freq<-ave(city_statistic$freq, city_statistic$group_final_id, FUN = sum)

这样每组的每个成员的记录行都会多一列相同的值,如上述函数按id汇总求freq的和,每一个id下的分店都有一个freq之和的属性

 

empty,character(0),NA,null
for character(0)
#rlang::is_empty()
 

 If

if (x< 0.2){
   x <- x+1
   print("success!")
}else{
   x < x-1
   print("else success!")
}
For
for (k in k:length(file)){}
how to build a function in R
##‘ gettianmap
##‘ 
##‘ Combine Repeated function
##‘ 
##‘ URLencode & getURL & fromJSON & tryCatch
##‘ 
##‘ @param url current URL address
##‘ @param timeout_set timeout value
##‘ @return amap
##‘ @author t.s.helianthus <\url{http://helianthus-code.lofter.com}> 
##‘ @examples  amap <- getMapData(url = ‘http://restapi.amap.com/v3/ip?key=389880a06e3f893ea46036f030c94700‘)
##‘ @import RCurl
##‘ @import rjson
##‘ @import httr
##‘ @export

gettianmap <- function (url, timeout_set = 15) {
  tianmap <- NULL;
  url <-  URLencode(enc2utf8(url))
  connect <- ‘‘
  # print(url)
  msg.load <- tryCatch ({
    connect <- getURL(url,httpheader=myheader,.encoding="utf-8", .opts = list(timeout = timeout_set, maxredirs = 2))
    tianmap <- fromJSON(connect)
    msg.load <- "TRUE"
  }, error = function (e) {
    "FALSE"
  }
  )
  if (msg.load == ‘FALSE‘) {
    print(‘error >> reload‘)
    msg.load <- tryCatch({
      tianmap <- content(GET(url,verbose(),timeout(timeout_set),add_headers(myheader)))
      msg.load <- "TRUE"
    }, error = function(e) {
      "FALSE"
    })
  }
  #if ( length(grep(‘too fast‘,amap)) == 1) {print(‘********too_fast********‘);print(stop_curl)}
  
  if ( length(grep(‘too fast‘,tianmap)) == 1) {print(‘********too_fast********‘);install.packages("ggplot2")}
  if (msg.load == ‘FALSE‘) {tianmap <- NULL;print(‘reload false >> NULL‘)}
  return(tianmap)
}
循环next和break
for(i in 1:5){ 
  if(i=3){
      break; 
  }
  print(i);
}
for(i in 1:5){ 
  if(i=3){
      next; 
  }
  print(i);
}
 

ggplot2

官方文档:https://ggplot2.tidyverse.org/reference/scale_manual.html

详细柱形图:https://www.r-bloggers.com/how-to-make-a-histogram-with-ggplot2/

添加横竖线:cnblogs.com/wkslearner/p/5697471.html

R cookbook:http://www.cookbook-r.com/Graphs/

所有关于坐标轴的信息:https://blog.csdn.net/Bone_ACE/article/details/47427453

查看图像的构造 ,如颜色

g <- ggplot_build(p)
unique(g$data[[1]]$color)

 

 

 

 

R语言基本操作

标签:plot   document   export   func   汇总   ack   ase   mem   tran   

原文地址:https://www.cnblogs.com/yuxuan320/p/12527194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!