说我想在ggplot中绘制两个图层,一个包含点,另一个包含线条,如果满足某个条件.
没有标准的代码可能如下所示:
library("ggplot2")
# Summarise number of movie ratings by year of movie
mry <- do.call(rbind, by(movies, round(movies$rating), function(df) {
nums <- tapply(df$length, df$year, length)
data.frame(rating=round(df$rating[1]), year = as.numeric(names(nums)), number=as.vector(nums))
}))
p <- ggplot(mry, aes(x=year, y=number, group=rating))
p +
geom_point()+
geom_line()
Run Code Online (Sandbox Code Playgroud)
现在绘制点而不仅仅是线条的条件是,一个名为tmp.data的对象不等于表达式"无值".
tmp.data<-c(1,2,3) # in this case the condition is fulfilled
# attempt to plot the two layers including the condition in the plotting function
p+
if(tmp.data[1]!="no value"){ geom_point()+}
geom_line()
Run Code Online (Sandbox Code Playgroud)
失败....
Error: unexpected '}' in:
"p+
if(tmp.data[1]!="no value"){ geom_point()+}"
Run Code Online (Sandbox Code Playgroud)
geom_line()geom_line: …
随着失踪信息学背景有困难明白之间的差别aes,并aes_string在GGPLOT2及其对日常使用影响.
从描述(?aes_string)我能够理解两者describe how variables in the data are mapped to visual properties (aesthetics) of geom.
此外,它说,aes uses non-standard evaluation to capture the variable names.而aes_string使用regular evaluation.
从示例代码可以看出,两者都产生相同的输出(a list of unevaluated expressions):
> aes_string(x = "mpg", y = "wt")
List of 2
$ x: symbol mpg
$ y: symbol wt
> aes(x = mpg, y = wt)
List of 2
$ x: symbol mpg
$ y: …Run Code Online (Sandbox Code Playgroud) 我尝试创建将两个不同的空间数据集组合在一起的摘要统计信息:一个大的栅格文件和一个多边形文件。这个想法是获得每个面内栅格值的摘要统计信息。
由于栅格太大而无法一次处理,因此我尝试创建子任务并并行处理它们,即一次处理每个多边形SpatialPolgyonsDataframe。
该代码工作正常,但是经过大约100次交互后,我遇到了内存问题。这是我的代码以及我打算做的事情:
# session setup
library("raster")
library("rgdal")
# multicore processing.
library("foreach")
library("doSNOW")
# assign three clusters to be used for current R session
cluster = makeCluster(3, type = "SOCK",outfile="")
registerDoSNOW(cluster)
getDoParWorkers()# check if it worked
# load base data
r.terra.2008<-raster("~/terra.tif")
spodf.malha.2007<-readOGR("~/,"composed")
# bring both data-sets to a common CRS
proj4string(r.terra.2008)
proj4string(spodf.malha.2007)
spodf.malha.2007<-spTransform(spodf.malha.2007,CRSobj = CRS(projargs = proj4string(r.terra.2008)))
proj4string(r.terra.2008)==proj4string(spodf.malha.2007) # should be TRUE
# create a function to extract areas
function.landcover.sum<-function(r.landuse,spodf.pol){
return(table(extract(r.landuse,spodf.pol)))}
# apply it one one subset to …Run Code Online (Sandbox Code Playgroud) 我可以解析一个html标签的完整参数,通过unix shell脚本解决它,如下所示:
# !/usr/bin/python3
# import the module
from bs4 import BeautifulSoup
# define your object
soup = BeautifulSoup(open("test.html"))
# get the tag
print(soup(itemprop="name"))
Run Code Online (Sandbox Code Playgroud)
其中itemprop="name"唯一标识所需的标记.
输出是这样的
[<span itemprop="name">
Blabla & Bloblo</span>]
Run Code Online (Sandbox Code Playgroud)
现在我想只返回Bla Bla Blo Blo部分.
我的尝试是:
print(soup(itemprop="name").getText())
Run Code Online (Sandbox Code Playgroud)
但我收到一条错误消息 AttributeError: 'ResultSet' object has no attribute 'getText'
它在其他环境中实验性地工作,例如
print(soup.find('span').getText())
Run Code Online (Sandbox Code Playgroud)
我错了什么?
我们假设以下示例:
test<-c(1:5,3:7)
Run Code Online (Sandbox Code Playgroud)
这使
test
[1] 1 2 3 4 5 3 4 5 6 7
Run Code Online (Sandbox Code Playgroud)
我想有一个简单的函数,如果值在向量中是唯一的,则返回TRUE.
[1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
Run Code Online (Sandbox Code Playgroud)
我尝试了什么:
unique(test)只给我一些独特的价值观,包括重复的价值观.
duplicated(test)让我回来
[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
这显然不是必需的结果,因为函数按顺序测试重复观察,第一次出现不算作重复.我当然可以通过包含fromLast = T和创建两个向量来反转控制序列.在这两个中,我可以创建一个表示真正独特值的第三个......但这很复杂.
table(test) 允许我计算每个值的出现次数
test
1 2 3 4 5 6 7
1 1 2 2 2 1 1
Run Code Online (Sandbox Code Playgroud)
这让我更接近我想要的,但仍然不是必需的结果(一个相同长度的向量,表明它是否在向量中是唯一的.)
所以任何人都知道如何更容易地做到这一点?
我有这样的矢量:
a <- c(11223344,55667788)
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的矢量切割,每个条目的最后两个数字在:
[1] 112233 556677
Run Code Online (Sandbox Code Playgroud)
我是否必须使用正则表达式来实现这一点,或者是否有一个我不知道的简单索引技巧?