概述: 我需要join两个表格: ref包含时间间隔(从t1到t2 )以及每个间隔的id和发生间隔的space 。 map包含时间间隔( t1到t2 ),每个时间间隔都有一个结果res和相应的space 。 我希望获得/join所有的间隔(和他们的分数)的map ,属于ref ref的时间间隔。 例: ref <- data.table(space=rep('nI',3),t1=c(100,300,500),t2=c(150,400,600),id=letters[1:3]) map <- data.table(space=rep('nI',241),t1=seq(0,1200,by=5),t2=seq(5,1205,by=5),res=rnorm(241)) 他们看着像是: > ref space t1 t2 id 1: nI 100 150 a 2: nI 300 400 b 3: nI 500 600 c > map space t1 t2 res 1: nI 0 5 -0.7082922 2: nI 5 […]
在R中将数字转换为基数2(在string中,例如5将被转换为"0000000000000101" )最简单的方法是什么? 有intToBits ,但它返回一个string的vector,而不是一个string: > intToBits(12) [1] 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [26] 00 00 00 00 00 00 00 我尝试了一些其他的function,但没有成功: > toString(intToBits(12)) [1] "00, 00, 01, 01, 00, 00, 00, 00, 00, 00, 00, […]
我有一堆列的数据表,例如: dt<-data.table(matrix(runif(10*10),10,10)) 我想对数据表执行一些操作,比如产生一个相关matrix( cor(dt) )。 为了做到这一点,我想删除一些包含非数字值或一定范围以外的值的列。 假设我想要find不包括V1,V2,V3和V5的相关matrix。 这是我目前的做法: cols<-!(colnames(dt)=="V1" | colnames(dt)=="V2" | colnames(dt)=="V3" | colnames(dt)=="V5") new_dt<-subset(dt,,cols) cor(new_dt) 我觉得这很麻烦,考虑data.table语法通常是如此的优雅。 有没有更好的方法来做到这一点?
我有一个data.frame ,我想写出来。 我的data.frame的维度是256行65536列。 write.csv什么更快的select?
我正试图产生一个低于10亿的素数列表。 我正在尝试,但这种结构是相当低劣的。 有什么build议么? a <- 1:1000000000 d <- 0 b <- for (i in a) {for (j in 1:i) {if (i %% j !=0) {d <- c(d,i)}}}
我最近在data.table发现了二进制search。 如果表格按多个键sorting,则可以仅在第二个键上search? DT = data.table(x=sample(letters,1e7,T),y=sample(1:25,1e7,T),rnorm(1e7)) setkey(DT,x,y) #R> DT[J('x')] # xy V3 # 1: x 1 0.89109 # 2: x 1 -2.01457 # — #384922: x 25 0.09676 #384923: x 25 0.25168 #R> DT[J('x',3)] # xy V3 # 1: x 3 -0.88165 # 2: x 3 1.51028 # — #15383: x 3 -1.62218 #15384: x 3 […]
以此示例variables df <- data.frame(month=rep(1:3,2), student=rep(c("Amy", "Bob"), each=3), A=c(9, 7, 6, 8, 6, 9), B=c(6, 7, 8, 5, 6, 7)) 我可以使用spread从tidyr改变这种广泛的格式。 > df[, -4] %>% spread(student, A) month Amy Bob 1 1 9 8 2 2 7 6 3 3 6 9 但是,我怎么能传播两个值,例如A和B ,这样的输出是类似的 month Amy.A Bob.A Amy.B Bob.B 1 1 9 8 6 5 2 […]
您好我试图命名variables使用for循环,所以我得到我的variables的dynamic名称。 for (i in 1:nX) { paste("X",i, sep="")=datos[,i+1] next }
我有一个字符数组 cf <- c("V440","V457","V116","V327","V446","V108", "V155","V217","V120","V51","V477") 我想按降序排列,这样我就可以得到这样的输出: V51 V108 V116 V120 V155 V217 V327 V440 V446 V457 V477 我已经尝试sort.list()像这样 cf[sort.list(cf)] 并得到了这个答案: [1] "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477" "V51" 也尝试了order()并得到了相同的结果。 有人可以帮我吗
我想通过给每个字符分配不同的值来做一些使用string的二维散步。 我打算“popup”一个string的第一个字符,使用它,并重复其余的string。 我怎么能做到这样的事情? x <- 'hello stackoverflow' 我想能够做到这样的事情: a <- x.pop[1] print(a) 'h' print(x) 'ello stackoverflow'