小编tlo*_*rin的帖子

在bash中获取变量类型

在Python中我可以通过以下方式获得变量类型

>>> i = 123
>>> type(i)
<type 'int'>
Run Code Online (Sandbox Code Playgroud)

我在这个页面上看到bash中没有变量类型.给出的解释是:

无类型变量既是祝福又是诅咒.它们允许更加灵活的脚本编写,并使编写代码行更容易(并给你足够的绳索自己挂起!).然而,它们同样允许微妙的错误蔓延并鼓励草率的编程习惯.

但我不确定它意味着什么,真正的优点(和缺点)是什么.

python variables bash types

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

R中的分组条形图带有误差条

我想绘制一个带有误差条的分组条形图.这是我能够达到的那种形象,这对我需要的东西是可以的:

在此输入图像描述

这是我的脚本:

#create dataframe
Gene<-c("Gene1","Gene2","Gene1","Gene2")
count1<-c(12,14,16,34)
count2<-c(4,7,9,23)
count3<-c(36,22,54,12)
count4<-c(12,24,35,23)
Species<-c("A","A","B","B")
df<-data.frame(Gene,count1,count2,count3,count4,Species)
df

mean1<-mean(as.numeric(df[1,][c(2,3,4,5)]))
mean2<-mean(as.numeric(df[2,][c(2,3,4,5)]))
mean3<-mean(as.numeric(df[3,][c(2,3,4,5)]))
mean4<-mean(as.numeric(df[4,][c(2,3,4,5)]))
Gene1SpeciesA.stdev<-sd(as.numeric(df[1,][c(2,3,4,5)]))
Gene2SpeciesA.stdev<-sd(as.numeric(df[2,][c(2,3,4,5)]))
Gene1SpeciesB.stdev<-sd(as.numeric(df[3,][c(2,3,4,5)]))
Gene2SpeciesB.stdev<-sd(as.numeric(df[4,][c(2,3,4,5)]))

ToPlot<-c(mean1,mean2,mean3,mean4)

#plot barplot
plot<-matrix(ToPlot,2,2,byrow=TRUE)   #with 2 being replaced by the number of genes!
tplot<-t(plot)
BarPlot <- barplot(tplot, beside=TRUE,ylab="count",
                names.arg=c("Gene1","Gene2"),col=c("blue","red"))

#add legend
legend("topright", 
       legend = c("SpeciesA","SpeciesB"), 
       fill = c("blue","red"))

#add error bars
ee<-matrix(c(Gene1SpeciesA.stdev,Gene2SpeciesA.stdev,Gene1SpeciesB.stdev,Gene2SpeciesB.stdev),2,2,byrow=TRUE)*1.96/sqrt(4)   
tee<-t(ee)
error.bar(BarPlot,tplot,tee)
Run Code Online (Sandbox Code Playgroud)

问题是我需要为50个基因和4个物种做这个,所以我的脚本会超级超长,我想这不是优化的......我试图在这里找到帮助,但我无法弄清楚做我想做的更好的方法.如果我不需要错误栏我可以调整这个脚本,但棘手的部分是混合ggplot美丽的条形图和错误栏!;)

如果你有任何想法来优化我的脚本,我真的很感激!:)

非常感谢!

r bar-chart ggplot2

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

Python从不同长度的多个列表中获取唯一对

假设在 Python 中我有 3 个列表:a, b, c可变长度。例如 :

a=[1,2,3]
b=[4,5,6]
c=[7,8]
Run Code Online (Sandbox Code Playgroud)

我想获得上面 3 个列表的两个元素的每个独特组合,即

[1,4],[1,5],[1,6],[1,7],[1,8],[2,4],[2,5]...并且不是 3 个列表的唯一组合(例如[1,4,7],[1,4,8],...)。

我已经看过这里解决方案,使用itertools它对 2 个列表来说非常好;但是,当包含n第一个列表时,此解决方案不再起作用,因为唯一组合的长度为n

这是我尝试过的:

import itertools

a=[1,2,3]
b=[4,5,6]
c=[7,8]

d=list(itertools.product(a,b,c))

[(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (1, 6, 7), (1, 6, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (2, 6, 7), (2, 6, 8), (3, 4, …
Run Code Online (Sandbox Code Playgroud)

python list python-itertools

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

R按条件行号选择多行

我有一个像这样的R数据帧:

a<-c(1,2,3,4,5)
b<-c(6,7,8,9,10)
df<-data.frame(a,b)
colnames(df)<-c("a","b")

df
  a  b
1 1  6
2 2  7
3 3  8
4 4  9
5 5 10
Run Code Online (Sandbox Code Playgroud)

我想获得列的第1行,第2行,第3行和第5行a,因此1 2 3 5,通过按行号选择行.

我试过df$a[1:3,5]但是我得到了Error in df$a[1:3, 5] : incorrect number of dimensions.什么工作,c(df$a[1:3],df$a[5])但我想知道是否有一个更简单的方法来实现这个与R?

row r dataframe

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

R散点图/气泡图,点大小基于观察数

我知道这个问题已经有所回答了这里,这里这里.

在所有这些示例中,点/气泡大小基于第三因素,例如size.

但是,在我的数据中,我只有2个变量,xvalyval.

library("ggplot2")
xval <- c("0","0.5","0.25","0","0")
yval <- c("1","0.5","0.25","0.25","1")
df.test <- data.frame(xval,yval)
df.test
p <- ggplot(df.test, aes(x = xval, y = yval)) + geom_point()
p
Run Code Online (Sandbox Code Playgroud)

这是 df.test

  xval yval
1    0    1
2  0.5  0.5
3 0.25 0.25
4    0 0.25
5    0    1
Run Code Online (Sandbox Code Playgroud)

这是 p

在此输入图像描述

我想要的是每个点/气泡大小取决于该坐标的观测值的出现次数.例如,(0,1)将是其他点的两倍大.我想避免在我的数据框中添加第3列,并让R自动执行.

我不知道这是否可以在不必过多地使用数据的情况下完成...任何见解都会非常感激:)

bubble-chart r scatter-plot ggplot2 dataframe

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

在Python for循环中跳过可变数量的迭代

我有一个列表和一个for循环,如下所示:

mylist = ['foo','foo','foo','bar,'bar','hello']
for item in mylist:
    cp = mylist.count(item)
    print("You "+item+" are present in "+str(cp)+" copy(ies)")
Run Code Online (Sandbox Code Playgroud)

输出:

You foo are present in 3 copy(ies)
You foo are present in 3 copy(ies)
You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You bar are present in 2 copy(ies)
You dude are present in 1 copy(ies)
Run Code Online (Sandbox Code Playgroud)

预期产量:

You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You dude are present …
Run Code Online (Sandbox Code Playgroud)

python for-loop continue

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

awk 替换行中的字符串

注意:这个问题看起来像以前发布的一个,但正如评论中提到的,它原来是一个变色龙问题。 我接受了答案,我在这里发布了同样的问题,但“解决方案条件”略有不同。


我有一个test.txt这样的文件(但包含更多行)

/foo/bar/how /SOME_TEXT_HERE/hello
/foo/bar/are hello/SOME_OTHER_TEXT
/foo/bar/you hello
Run Code Online (Sandbox Code Playgroud)

我想得到这个输出:

/foo/bar/how /SOME_TEXT_HERE/how
/foo/bar/are are/SOME_OTHER_TEXT
/foo/bar/you you
Run Code Online (Sandbox Code Playgroud)

我试过这个:

while read line
do
bla=$(echo $line | cut -f4 -d"/" | cut -f1 -d" ")
sed -i "s/hello/$bla/" test.txt
done <test.txt
Run Code Online (Sandbox Code Playgroud)

但输出是:

/foo/bar/how /SOME_TEXT_HERE/how
/foo/bar/are how/SOME_OTHER_TEXT
/foo/bar/you how
Run Code Online (Sandbox Code Playgroud)

注意

我想解决以下问题:

  1. 允许我定义一个变量(这里,bla),我将手动定义一个文件到另一个文件(所以不要像基本字段位置那样使用 sthg),而是cut像我的例子一样使用或 其他命令)

  2. 用这个变量替换行中其他地方的特定字符串(所以不是像 那样的字段位置$2,而是像 那样s/hello/$bla

我不确定这是否可行(显然我自己不知道如何做到这一点......),但感谢您的时间尝试!:)

bash awk sed

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

在R中的文件路径中使用变量

在bash我可以这样做:

BLA=some/directory 然后

MyFavoriteFile1=/some/path/to/$BLA/myfile1.someextension
MyFavoriteFile2=/some/path/to/$BLA/myfile2.someextension
Run Code Online (Sandbox Code Playgroud)

我想知道R中是否存在相同的情况?所以我只需要BLA在整个脚本中更改一次.

bash r path

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

将字典写入csv,每个值一行

我是Python的新手,所以请原谅我这是一个非常基本的问题.我有一个像这样的Python字典:

foo = {'bar1':['a','b','c'], 'bar2':['d','e']}

我想把它写成一个csv文件,每个一行,键作为每行的第一个元素.输出将是(无论引号):

bar1,'a'
bar1,'b'
bar1,'c'
bar2,'d'
bar2,'e'
Run Code Online (Sandbox Code Playgroud)

我试过这样的建议

import csv
with open('blah.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file)
    for key, value in foo.items():
       writer.writerow([key, value])
Run Code Online (Sandbox Code Playgroud)

但这会给出以下输出,每个一行:

bar1,"['a', 'b', 'c']"
bar2,"['d', 'e']"
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

python csv dictionary file key

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