(这一定是一个非常基本的问题,但到目前为止我没有在R手册中找到答案......)
当我使用lapply语法时lapply- 这很容易理解,我可以像这样定义myfun:
myfun <- function(x) {
# doing something here with x
}
lapply(input, myfun);
Run Code Online (Sandbox Code Playgroud)
和元素R作为lapply(input, myfun);参数传递给input.
但是,如果我需要传递更多参数x呢?例如,它定义如下:
myfun <- function(x, arg1) {
# doing something here with x and arg1
}
Run Code Online (Sandbox Code Playgroud)
如何使用此函数传递两个myfun元素(作为myfunc参数)和其他一些参数?
我有一系列有序点,如下所示:

但是,当我尝试通过一行连接点时,我得到以下输出:

该图连接26到1和25到9和10(一些错误),而不是遵循顺序.绘制点的代码如下:
p<-ggplot(aes(x = x, y = y), data = spat_loc)
p<-p + labs(x = "x Coords (Km)", y="Y coords (Km)") +ggtitle("Locations")
p<-p + geom_point(aes(color="Red",size=2)) + geom_text(aes(label = X))
p + theme_bw()
Run Code Online (Sandbox Code Playgroud)
并绘制我正在使用的线:p + geom_line((aes(x = x,y = y)),color ="blue")+ theme_bw()
包含位置的文件具有以下结构:
p +
geom_line((aes(x=x, y=y)),colour="blue") +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
其中X是数字ID,x和y是坐标对.
我需要做什么才能使线条遵循点的顺序?
我有一个列表列表,每个列表包含不同数量的字符串.我想(有效地)将这些全部转换为整数,但感觉有点密集,因为我无法让它为我的生活而努力.我一直在尝试:newVals = [int(x)for x in [row for values in values]]
"值"是列表的列表.它一直说x是一个列表,因此如果是int()则不能成为参数.显然我在这里做了些蠢事,这是什么?对于这种事情,有没有被接受的习语?
像往常一样,提前感谢.
目的
我正在尝试将 的形状更改geom_point为十字架(因此不是“加/加”符号,而是“死亡”十字架)。
试图
假设我有以下数据:
library(tidyverse)
df <- read.table(text="x y
1 3
2 4
3 6
4 7 ", header=TRUE)
Run Code Online (Sandbox Code Playgroud)
我可以使用shape参数 in将形状更改geom_point为不同的形状,如下所示:
ggplot(data = df, aes(x =x, y=y)) +
geom_point(shape=2) # change shape
Run Code Online (Sandbox Code Playgroud)
但是,没有将形状更改为十字的选项。
题
如何使用 R 中的 ggplot 将值的形状更改为十字形?
作为 C++ 的完全初学者,我想从正态分布生成一个随机数。
使用以下代码(源自这篇文章),我可以做到这一点:
#include <iostream>
#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>
using namespace std;
int main()
{
boost::mt19937 rng(std::time(0)+getpid());
boost::normal_distribution<> nd(0.0, 1.0);
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > rnorm(rng, nd);
cout<< rnorm();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于代码非常复杂(在我看来),我认为可能有一个更简单的解决方案:
#include <iostream>
#include <random>
using namespace std;
int main()
{
default_random_engine generator;
normal_distribution<double> distribution(0.0,1.0);
cout << distribution(generator);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然我可以生成一个随机数,但它始终是相同的数字。这引出了两个问题:
(1) 为什么会发生这种情况以及如何解决这个问题?
(2) 还有其他更简单的方法来生成随机数吗?
在使用purrr中的 Tidyverse 包时R,我看到该map()函数返回一个列表。
library(tidyverse)
set.seed(123)
map(1:5, ~rnorm(3))
#> [[1]]
#> [1] -0.5604756 -0.2301775 1.5587083
#>
#> [[2]]
#> [1] 0.07050839 0.12928774 1.71506499
#>
#> [[3]]
#> [1] 0.4609162 -1.2650612 -0.6868529
#> ......
Run Code Online (Sandbox Code Playgroud)
我想将此列表转换为包含 3 列的数据框。一种选择是使用do.call(rbind, .). 不过,我也注意到这个map_dfr()功能的存在。
以与 相同的方式使用此函数map()会产生错误。
map_dfr(1:5, ~rnorm(3))
#> Error: Argument 1 must have names.
Run Code Online (Sandbox Code Playgroud)
问题
导致此错误的函数map()和函数之间有什么区别?map_dfr()您应该如何使用该map_dfr()函数直接在映射函数中绑定行?
我有一个看起来像这样的数据集,
df<-structure(list(Year = c(1985L, 1986L, 1987L, 1988L, 1989L, 1990L,
1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L,2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, …Run Code Online (Sandbox Code Playgroud) 虽然我已经找到了关于 scipy.ndimage.convolve 函数的文档并且我“实际上知道它是做什么的”,但当我尝试计算结果数组时,我无法遵循数学公式。让我们举个例子:
a = np.array([[1, 2, 0, 0],`
[5, 3, 0, 4],
[0, 0, 0, 7],
[9, 3, 0, 0]])
k = np.array([[1,1,1],[1,1,0],[1,0,0]])
from scipy import ndimage
ndimage.convolve(a, k, mode='constant', cval=0.0)
# Why is the result like this ?
array([[11, 10, 7, 4],
[10, 3, 11, 11],
[15, 12, 14, 7],
[12, 3, 7, 0]])
Run Code Online (Sandbox Code Playgroud)
我将不胜感激逐步计算。
我正在尝试sf通过应用st_simplify. CRS 是 4267 并尝试使用正确的dTolerance. 我知道单位dTolerance必须是 CRS 的单位,所以我从 0.1 开始,但我不断收到此错误消息。
test <- st_read("comm_sf.shp") %>%
+ st_simplify(preserveTopology = T,
+ dTolerance = 0.1)
Simple feature collection with 11321 features and 21 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -124.4375 ymin: 24.5441 xmax: -66.94983 ymax: 49.00249
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
Warning message:
In st_simplify.sfc(st_geometry(x), preserveTopology, dTolerance) :
st_simplify does not correctly simplify longitude/latitude data, dTolerance needs to be in decimal …Run Code Online (Sandbox Code Playgroud) 我想对齐两个面板图,其中的一个产生facet_wrap于ggplot以下方式(注:A组需要留空):
但是,我注意到面板 B 的 y 轴与从最后一个到面板 C 的绘图的 y 轴不完全对齐(尽管axis = 'lb'在两个方面都有选项)。
代码
# Load libraries
library(tidyverse)
library(cowplot)
# Create two facetted plots
p1 <- ggplot(data = diamonds, aes(x = carat, y = price)) +
facet_wrap(~ cut, scales = "free_y", ncol = 5) +
geom_point(size=0.5)
p2<- ggplot(data = filter(diamonds, price < 900 & (cut == "Fair" | cut == "Good" )), aes(x = carat, y = price)) +
facet_wrap(~ cut, scales = "free_y", ncol …Run Code Online (Sandbox Code Playgroud)