小编And*_*ndy的帖子

在R中使用具有多个向量的same()

假设我有五个向量:

A<-1:10
B<-1:10
C<-1:10
D<-1:10
E<-1:12
Run Code Online (Sandbox Code Playgroud)

我可以使用相同的()一次测试两个.

identical(A,C)
Run Code Online (Sandbox Code Playgroud)

但我想立刻测试所有这些,看看是否有任何不同于其他的.是否有捷径可寻?

r

31
推荐指数
3
解决办法
6091
查看次数

通过R建立到另一台计算机的SSH隧道以访问postgreSQL表

作为我的一个项目的R工作流程的一部分,我从位于远程服务器上的postgreSQL表中加载数据.

我的代码看起来像这样(匿名凭证).

我首先打开一个到终端远程服务器的ssh连接.

ssh -p Port -L LocalPort:IP:RemotePort servername"
Run Code Online (Sandbox Code Playgroud)

然后我连接到R中的postgres数据库.

# Load the RPostgreSQL package
library("RPostgreSQL")

# Create a connection
Driver <- dbDriver("PostgreSQL") # Establish database driver
Connection <- dbConnect(Driver, dbname = "DBName", host = "localhost", port = LocalPort, user = "User")

# Download the data
Data<-dbGetQuery(Connection,"SELECT * FROM remote_postgres_table")
Run Code Online (Sandbox Code Playgroud)

这种方法工作正常,我可以毫无问题地下载数据.

但是,我想在R中而不是在终端中执行第一步 - 即创建ssh连接.这是我尝试这样做的,伴随着错误.

# Open the ssh connection in R
system("ssh -T -p Port -L LocalPort:IP:RemotePort servername")

# Load the RPostgreSQL package
library("RPostgreSQL")

# Create a connection …
Run Code Online (Sandbox Code Playgroud)

ssh r

11
推荐指数
1
解决办法
3015
查看次数

抑制 R 中的进度条

R的eHOF包中的HOF函数会自动生成进度条。函数中没有关闭进度条的参数。

有什么办法可以抑制函数外的进度条的生成?(我正在应用该功能数百次,目前进度条除了淹没我的屏幕外没有任何作用。)

r progress-bar

4
推荐指数
1
解决办法
1233
查看次数

在R向量中找到可能完全为正的第一个负数的位置

假设我有一个从正态分布中得出的向量。

RandomVector<-rnorm(10,mean=1,sd=1)
> RandomVector
[1]  1.18186018  0.53810223  0.33888370  0.46593762  2.33963330 -0.08779368     -0.65178144 -0.61384231 -0.74351425
[10] -0.51577616
Run Code Online (Sandbox Code Playgroud)

我想找到向量中第一个数字<= 0 的位置。最简单的方法是使用它。

FirstNegative<-min(which(RandomVector<=0))
Run Code Online (Sandbox Code Playgroud)

但是,在某些情况下,向量可能没有负数,在这种情况下,which()将返回长度为零的向量。

> RandomVector
 [1] 1.20150245 0.58668248 1.65633049 1.63277953 0.51134272 0.76385987 0.89085391 0.06504819 0.18011920 0.02400666
Run Code Online (Sandbox Code Playgroud)

这会给你一个错误。

> FirstNegative<-min(which(RandomVector<0))
Warning message:
In min(which(RandomVector < 0)) :
  no non-missing arguments to min; returning Inf
Run Code Online (Sandbox Code Playgroud)

这是我想出的一些解决方案。它们都可以工作,但我想尽可能避免使用if / else语句。谁能想到更简洁的方法?

for (j in 1:length(RandomVector)) {
        if (RandomVector[j]<=0) {
            FirstNegative<-j
            break;
            }
Run Code Online (Sandbox Code Playgroud)

或者...

WhichNegative<-which(RandomVector<=0)
if (length(WhichNegative)>0) {
    FirstNegative<-min(WhichNegative)
    }
Run Code Online (Sandbox Code Playgroud)

基本思路相同,但仍使用if语句

 if (any(RandomVector<=0)==TRUE) {
     FirstNegative<-min(which(RandomVector<=0))
     } …
Run Code Online (Sandbox Code Playgroud)

r

2
推荐指数
1
解决办法
3977
查看次数

线条的不同颜色,而不是 R 中的点

我有一个笛卡尔坐标数据集。我想制作一个基本的散点图,其中每个点都根据预设的颜色向量进行着色。这很容易通过以下示例实现。

# A hypothetical dataset
XCoords<-c(1,3,5,6,8)
YCoords<-c(3,9,4,3,4)

# Sorry for picking such ugly colors, its just an example
Colors<-c("#FDA75F","#F1E19D","#E5AC4D","#FDC07A","#FDB46C")

# Plot the scatter plot
plot(x=XCoords,y=YCoords,pch=16,col=Colors)
Run Code Online (Sandbox Code Playgroud)

但是,如果我想使用 type="o" 或 type="b"(取决于系统)在点之间绘制一条线,则该线的颜色将默认为Colors向量中的第一种颜色。

# An example using type="o"
plot(x=XCoords,y=YCoords,pch=16,col=Colors,type="o")
Run Code Online (Sandbox Code Playgroud)

如果我希望线条是完全不同的颜色,例如黑色怎么办?换句话说,如何将连接点的线的颜色设置为独立于点的配色方案。

我专门在 base(没有 ggplot)中寻找解决此问题的方法,理想情况下,它不会要求我在两个单独的步骤中绘制线条和点(尽管如果这是唯一的方法,那也没关系)。

plot r

1
推荐指数
1
解决办法
8198
查看次数

标签 统计

r ×5

plot ×1

progress-bar ×1

ssh ×1