我正在使用一个需要输入为整数的函数.
所以我一直在尝试阅读整数:
y <- 3.14
as.integer(y)
[1] 3 # all cool
Run Code Online (Sandbox Code Playgroud)
一切都很好,但如果我有
x <- 1639195531833
as.integer(x)
[1] NA
Warning message:
NAs introduced by coercion
Run Code Online (Sandbox Code Playgroud)
我曾经options(digits = 15)和它混淆了为什么它不起作用,但在一个干净的会议中它必须与科学记数法.
我也试图欺骗R,但不高兴:
as.integer(as.character(x))
[1] 2147483647
Warning message:
inaccurate integer conversion in coercion
Run Code Online (Sandbox Code Playgroud)
我如何击败科学记数法并得到我的整数?
df <- structure(list(x = 1:10, time = c(0.5, 0.5, 1, 2, 3, 0.5, 0.5,
1, 2, 3)), .Names = c("x", "time"), row.names = c(NA, -10L), class = "data.frame")
df[df$time %in% c(0.5, 3), ]
## x time
## 1 1 0.5
## 2 2 0.5
## 5 5 3.0
## 6 6 0.5
## 7 7 0.5
## 10 10 3.0
df[df$time == c(0.5, 3), ]
## x time
## 1 1 0.5
## 7 7 0.5
## 10 10 3.0 …Run Code Online (Sandbox Code Playgroud) 如果我有这些字符串:
mystrings <- c("X2/D2/F4",
"X10/D9/F4",
"X3/D22/F4",
"X9/D22/F9")
Run Code Online (Sandbox Code Playgroud)
我怎样才能提取出来2,9,22,22.这些字符/位于第一个字符之间和之后/.
我想以矢量化方式执行此操作,并添加新列,transfrom如果可能的话,我熟悉它.
我认为这个正则表达式让我在附近的所有角色附近\:
^.*\\'(.*)'\\.*$
Run Code Online (Sandbox Code Playgroud) 先前的回答另一个问题提供了一些代码来重新连接一个加权图如下:
g <- graph.ring(10)
E(g)$weight <- seq_len(ecount(g))
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
is.weighted(g)
# [1] TRUE
g2 <- rewire(g,niter=3)
plot(g2)
is.weighted(g2)
# [1] TRUE
Run Code Online (Sandbox Code Playgroud)
但是,使用此代码可以看出NAs被引入到边权重向量中:
set.seed(1)
g <- graph.ring(10)
E(g)$weight <- seq_len(ecount(g))
E(g)$weight
# [1] 1 2 3 4 5 6 7 8 9 10
is.weighted(g)
# [1] TRUE
g2 <- rewire(g,niter=3)
E(g2)$weight
# [1] 1 2 4 5 6 7 9 NA NA NA
is.weighted(g2)
# [1] …Run Code Online (Sandbox Code Playgroud) 这样可以在一段时间之前保留所
gsub("\\..*","", data$column )
Run Code Online (Sandbox Code Playgroud)
如何保持一段时间后的一切?
我发现这个答案非常有用.它可以帮助我绘制网络/图形并选择图中节点的坐标.
但是,布局将坐标重新调整为-1到1.首先,我试图找出它是如何做到这一点但不能.它做了这样的事吗?
(coordinate - mean(coordinates))/(coordinate + mean(coordinates)
Run Code Online (Sandbox Code Playgroud)
第二是有办法保持原始坐标吗?我喜欢用图表制作轴,所以不希望重新调整一切.
如果我做一个图表g:
g <- read.table(text="
A B W
1 55 3
2 55 5
3 99 6 ",header=TRUE)
library(igraph)
g <- graph.data.frame(g)
Run Code Online (Sandbox Code Playgroud)
和坐标矩阵:
y<-1:5
x<-c(0.1,0.1,0.2,0.2,0.8)
l<-data.frame(x,y)
l<-as.matrix(l)
Run Code Online (Sandbox Code Playgroud)
我可以根据自定义坐标和绘图轴绘制具有节点位置的图形.
plot(g,layout=l,rescale=F,axes=TRUE,ylim=c(0,6),xlim=c(0,1))
Run Code Online (Sandbox Code Playgroud)

但是xaxis限制功能不正常,我认为是由yaxis限制改变的.我如何以他想要的方式控制x轴,例如将其保持在0和1之间.
即 plot(x,y,xlim=c(0,1),ylim=c(0,6))
这是一个错误吗?如果是,并且这无法解决是否有另一个具有相同功能的包?
想象一下,我们正在做一些过程,我想在程序的开头设置一个整体种子:例如
mylist <- list( as.list(rep(NA,3)), as.list(rep(NA,3)) )
foo <- function(x){ for(i in 1:length(x)){
x[[i]] <- sample(100,1)
}
return(x)
}
# start block
set.seed(1)
l1 <- lapply(mylist, foo)
l2 <- lapply(mylist, foo)
# end
Run Code Online (Sandbox Code Playgroud)
当然在一个区块内l1并且l2是不同的,但是如果我再次运行上面的区块l1将与之前相同并且l2将与之前相同.
想象一下,foo是可怕的耗时,所以我想用mclapply没有lapply,所以我这样做:
library(parallel)
# start block
set.seed(1)
mclapply(mylist , foo, mc.cores = 3)
mclapply(mylist , foo, mc.cores = 3)
# end
Run Code Online (Sandbox Code Playgroud)
如果我再次运行此块,我将在下次获得不同的结果.我如何产生与使用lapply但设置一个整体种子相同的行为mclappy.我查看了mclapplydoc,但我不确定因为使用:
set.seed(1)
l1 <- mclapply(mylist , …Run Code Online (Sandbox Code Playgroud) 我有df类型和价值观.我想按x内部顺序对它们进行排序,type并计算有多少其他行的行n数高于x(列pos).
例如
df <- data.frame(type = c("a","a","a","b","b","b"),x=c(1,77,1,34,1,8))
# for type a row 3 has a higher x than row 1 and 2 so has a pos value of 2
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
library(plyr)
df <- data.frame(type = c("a","a","a","b","b","b"),x=c(1,77,1,34,1,8))
df <- ddply(df,.(type), function(x) x[with(x, order(x)) ,])
df <- ddply(df,.(type), transform, pos = (seq_along(x)-1) )
type x pos
1 a 1 0
2 a 1 1
3 a 77 2
4 b …Run Code Online (Sandbox Code Playgroud) 我想使用一个快速给出矢量广告标准偏差的函数,允许我在矢量中包含元素的权重.即
sd(c(1,2,3)) #weights all equal 1
#[1] 1
sd(c(1,2,3,3,3)) #weights equal 1,1,3 respectively
#[1] 0.8944272
Run Code Online (Sandbox Code Playgroud)
为加权装置I可以使用wt.mean()从library(SDMTools)例如
> mean(c(1,2,3))
[1] 2
> wt.mean(c(1,2,3),c(1,1,1))
[1] 2
>
> mean(c(1,2,3,3,3))
[1] 2.4
> wt.mean(c(1,2,3),c(1,1,3))
[1] 2.4
Run Code Online (Sandbox Code Playgroud)
但wt.sd功能似乎没有提供我想要的东西:
> sd(c(1,2,3))
[1] 1
> wt.sd(c(1,2,3),c(1,1,1))
[1] 1
> sd(c(1,2,3,3,3))
[1] 0.8944272
> wt.sd(c(1,2,3),c(1,1,3))
[1] 1.069045
Run Code Online (Sandbox Code Playgroud)
我期待一个0.8944272从我加权返回的函数sd.我最好在data.frame上使用它,如:
data.frame(x=c(1,2,3),w=c(1,1,3))
Run Code Online (Sandbox Code Playgroud)