如果我有以下数据帧:
value factorA factorB
1 a e
2 a f
3 a g
1 b k
2 b l
3 b m
1 c e
2 c g
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到每个因子A的最高值和与之相关的因子B的条目,即
value factorA factorB
3 a g
3 b m
2 c g
Run Code Online (Sandbox Code Playgroud)
如果没有先使用,这是否可行
blocks<-split(factorA, list(), drop=TRUE)
Run Code Online (Sandbox Code Playgroud)
然后对每个块$ a进行排序,因为这将执行多次,并且块的数量将始终改变.
我不确定执行此操作的最佳方法,但我有一个保存为 .py 的 python 脚本。该脚本的最终输出是两个文件 x1.txt 和 y1.txt。
基本上我想运行这个脚本1000次,每次运行用新名称写入我的两个文本文件,即x1.txt + y1.txt,然后第二次运行x2.txt和y2.txt。
考虑到这一点,似乎最好用类似的东西开始整个脚本
runs=xrange(:999)
for i in runs:
##run the script
Run Code Online (Sandbox Code Playgroud)
然后完成一些事情
for i in runs:
filnameA=prefix += "a"+i
open("filnamea.txt", "w").write('\n'.join('\t'.join(x for x in g if x) for g in grouper(7, values)))
for i in runs:
filnameB=prefix += "a"+i
open("filnameB.txt", "w").write('\n'.join('\t'.join(x for x in g if x) for g in grouper(7, values)))
Run Code Online (Sandbox Code Playgroud)
这真的是最好的方法吗?我敢打赌这不是......更好的想法?
我知道您可以import time编写一个与时间相关的文件名,但这对于以后的处理来说会很烦人。
我有这些数据:
thedat <- structure(list(id = c(" w12", " w12", " w12",
" w11", " w3", " w3", " w12",
" w45", " w24", " w24", " w24", " w08",
" w3", " w3", " w11"), time = structure(c(1559329080,
1559363580, 1559416140, 1559329380, 1559278020, 1559413920, 1559285100,
1559322660, 1559417460, 1559450220, 1559500980, 1559500980, 1559278020,
1559413920, 1559276700), class = c("POSIXct", "POSIXt"), tzone = ""),
x = c(28.03333, 18.45, 3.85, 27.95, 42.216667, 4.466667,
64.25, 53.81667, 27.483333, 18.383333, 4.283333, 4.28333336,
66.21667, 28.46667, 66.58333)), .Names = c("id", "time", …Run Code Online (Sandbox Code Playgroud) 如果我有以下数据框G:
z type x
1 a 4
2 a 5
3 a 6
4 b 1
5 b 0.9
6 c 4
Run Code Online (Sandbox Code Playgroud)
我想得到:
z type x y
3 a 6 3
2 a 5 2
1 a 4 1
4 b 1 2
5 b 0.9 1
6 c 4 1
Run Code Online (Sandbox Code Playgroud)
即我想type基于向量在因子水平内对整个数据帧进行排序x.获取每个级别的长度,a = 3 b=2 c=1然后在新向量中以递减的方式编号y.
我的出发地目前是 sort()
tapply(y, x, sort)
Run Code Online (Sandbox Code Playgroud)
首先尝试使用sapply将所有内容分开是否最好?
我有以下的list1和list2:
df1 <- data.frame(x=(1:3),Q=(3:5))
df2 <- data.frame(x=(1:3),Q=(3:5))
df3 <- data.frame(x=(1:3),Q=(3:5))
list1 <- list(df1,df2,df3)
list2 <- list(2,3,6)
Run Code Online (Sandbox Code Playgroud)
我想根据相应的值从Q每个list1元素中随机抽样
list2
所以我会Q为第一对列表元素抽样2次.
到目前为止,我已经管理:
df1 <- data.frame(x=(1:3),Q=(3:5))
z <- 2
sapply(1:z,function(i) sample(df1$Q,1))
Run Code Online (Sandbox Code Playgroud)
但我正在努力尝试mapply这两个列表中的所有元素对.
如果我想对两个向量中的所有元素进行编号,向量 1 获取所有奇数,向量 2 获取所有偶数,假设向量的长度为 10,我可以这样做。
seq(1, 10, by=2)
[1] 1 3 5 7 9
seq(2, 11, by=2)
[1] 2 4 6 8 10
Run Code Online (Sandbox Code Playgroud)
但如果我的向量只有一个元素,我会遇到问题:
seq(2)
[1] 1 2
Run Code Online (Sandbox Code Playgroud)
所以我用:
seq_along(2)
[1] 1
Run Code Online (Sandbox Code Playgroud)
但我不能by=在seq_long(). seq_along我如何获得的功能的可靠性seq()?
这个例子可能会澄清一些事情。
想象一下我有两个列表:
list1 <- list(4)
list2 <- list(4)
Run Code Online (Sandbox Code Playgroud)
list1必须沿着列表的元素获取偶数名称。
list2列表元素中必须有奇数名称。
我不知道列表元素有多长。
seq_along(list1[[1]]) # this will know to only give one name but I cant make it even
seq(list2[[1]]) # this know to give 1 name
#and …Run Code Online (Sandbox Code Playgroud) 我有一个list的data.frames.每个data.frame都不是很大~15万行.但我的名单中有超过1000个data.frames.
一个data.frame样子:
comp <- read.table(text = " G T H S B
1 1 1 1 x1
1 1 1 2 x2
1 2 6 1 x3
1 2 6 2 x4
2 1 7 1 x1
2 2 8 2 x2
2 2 8 1 x1
2 3 9 2 x2",header=TRUE,stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)
所以列表是:
complist <- list(comp,comp,comp)
Run Code Online (Sandbox Code Playgroud)
我想知道的每一个data.frame,( comp),长度B为每个S每个H每个T每个G …
从以下列表中获取最快捷的方法是什么:
set.seed(1)
mylist <- list(1:2,1:3,1:4)
names(mylist) <- sample(LETTERS,3)
mylist
$J
[1] 1 2
$V
[1] 1 2 3
$Z
[1] 1 2 3 4
Run Code Online (Sandbox Code Playgroud)
到包含列表名称的额外向量的列表,即
mylist
$J
[1] 1 2
[2] J J
$V
[1] 1 2 3
[2] V V V
$Z
[1] 1 2 3 4
[2] Z Z Z Z
Run Code Online (Sandbox Code Playgroud)
结果同样可以是数据帧列表.某种
lapply(mylist, function(x) { data.frame(x=x,y=names(x)})
Run Code Online (Sandbox Code Playgroud) 我试图在R曲线下绘制正态分布和阴影,但我在曲线下方遇到了不必要的问题.
x <- seq(-3,3,0.0001)
y <- dnorm(x,0,1)
ytop <- dnorm(-3,3,0.001)
df <-data.frame(x=x,y=y)
p <- ggplot(df, aes(x=x,y=y)) + geom_line()
cover <- rbind(c(1,0), subset(df, x > 1), c(df[nrow(df), "X"], 0))
p + geom_segment(aes(x=1,y=0, xend=2.5, yend=ytop)) +
geom_polygon(data = cover, aes(x, y))
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么我弄乱这个数字并在阴影区域下方划线?

假设我有这些数据:
d1 <- data.frame(x = letters[1:3], y=LETTERS[24:26], num = 1:3)
d2 <- data.frame(x = letters[1:3], y=LETTERS[24:26], num = c(1,2,30))
library(gridExtra)
library(ggplot2)
ggd1 <- ggplot(d1, aes(x=x,y=y)) +
geom_tile(aes(fill=num)) +
scale_fill_gradient(low = "green", high = "blue")
ggd2 <- ggplot(d2, aes(x=x,y=y)) +
geom_tile(aes(fill=num)) +
scale_fill_gradient(low = "green", high = "blue")
grid.arrange(ggd1,ggd2)
Run Code Online (Sandbox Code Playgroud)

我的问题是如何规范填写梯度,这样即使在D1和D2的数据的程度不同,颜色X-a,Y-b768,16匹配两条曲线,但Z-c应该由一个数量级的差别.即我希望两个图都保持相同的比例.
任何想法如何打印
(([0.2, 0.6], [0.5, 0.8]), "10 11 {'chocolate': 10}")
(([0.0, 0.6], [0.8, 0.5]), "10 12 {'chocolate': 10}")
Run Code Online (Sandbox Code Playgroud)
如..
0.2 0.6 0.5 0.8 10 11 chocolate 10
0.0 0.6 0.8 0.5 10 12 chocolate 10
Run Code Online (Sandbox Code Playgroud)
我也设法强迫它(暂时忽略巧克力):
(([0.2, 0.6], [0.5, 0.8]), '10 11 10')
(([0.0, 0.6], [0.8, 0.5]), '10 12 10')
Run Code Online (Sandbox Code Playgroud)
如果这更容易变成
0.2 0.6 0.5 0.8 10 11 10
0.0 0.6 0.8 0.5 10 12 10
Run Code Online (Sandbox Code Playgroud)
尝试了我在其他问题上找到的但是比如
print '\n'.join('\t'.join(map(str,tuple(chain(*i)))) for i in s)
Run Code Online (Sandbox Code Playgroud)
这导致单词和最后的元素看起来像
c h o c o l a t …Run Code Online (Sandbox Code Playgroud) 我不使用那么多函数,但当我使用时,我倾向于使用匿名函数和某种形式的apply. 然而,我现在正在尝试编写一个可以处理列表中的项目的函数。
有两个列表,每个列表都有许多项目(我所说的项目是指例如mylist1[1])。所有项目都是数据框。我想从中获取第一个数据帧mylist1和第一个数据帧,mylist2并在这些数据帧中的列上运行一堆函数。然后取出第二mylist1项和第二mylist2项,依此类推......
下面是我习惯写的东西,但在这种情况下显然不适用于两个列表。任何人都可以帮我找到一种快速的方法来弄清楚我应该如何使用似乎sapply导致主要问题的方法以外的方法来解决这个问题。
a <- c(1:10)
b <- c(1:10)
z <- c(rep("x", 5), rep("y", 5))
df <- data.frame(cbind(a, b, z))
mylist1 <- split(df, z)
mylist2 <- split(df, z)
myfunction <- function(x, y)
{
a <- as.data.frame(x[1])
b <- as.data.frame(y[1])
meana <- mean(a[1])
meanb <- mean(b[1])
model <- lm(a[1]~b[1])
return(c(model$coefficients[2], meana, meanb))
}
result <- sapply(mylist1, mylist2, myfunction)
Run Code Online (Sandbox Code Playgroud)
subset我还只是想人们是否认为这样做会z比split这样做更好?