假设我们有一个数据帧
x y
1 1
2 4
4 5
Run Code Online (Sandbox Code Playgroud)
如何在数据框中添加一个新变量,使得如果x小于或等于1,则返回"good",如果x介于3和5之间,则返回"bad",否则返回"fair"
x y w
1 1 "good"
2 2 "fair"
5 5 "bad"
Run Code Online (Sandbox Code Playgroud)
应用了ocram.所示的方法,但是这里的方法不起作用.
d1 <- c("e", "c", "a")
d2 <- c("e", "a", "b")
w <- ifelse(d1 == "e" & (d2=="e"), 1, ifelse((d1 == "a") & (d2 =="b"), 2, ifelse(d1 == "e"),3,99))
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢
我是D的新手,想要解析表格的生物文件
>name1
acgcgcagagatatagctagatcg
aagctctgctcgcgct
>name2
acgggggcttgctagctcgatagatcga
agctctctttctccttcttcttctagagaga
>name2
gag ggagag
Run Code Online (Sandbox Code Playgroud)
这样我就可以捕获'header'name1,name2,name3以及相应的'sequence'数据,..acgcg ... stuff.
现在我有了这个.但它只会逐行迭代,
import std.stdio;
import std.stream;
import std.regex;
int main(string[] args){
auto filename = args[1];
auto entry_name = regex(r"^>(.*)"); //captures header only
auto fasta_regex = regex(r"(\>.+\n)([^\>]+\n)"); //captures header and correponding sequence
try {
Stream file = new BufferedFile(filename);
foreach(ulong n, char[] line; file) {
auto name_capture = match(line,entry_name);
writeln(name_capture.captures[1]);
}
file.close();
}
catch (FileException xy){
writefln("Error reading the file: ");
}
catch (Exception xx){
writefln("Exception occured: " ~ …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的数据框.
input dataframe
position,mean_freq,reference,alternative,sample_id
1,0.002,A,C,name1
2,0.04,G,T,name1
3,0.03,A,C,name2
Run Code Online (Sandbox Code Playgroud)
这些数据是在一个假设的基因组中的给定位置的核苷酸差异,mean_freq是相对于参考,所以第一行指的比例C's被0.002暗示A是在0.998.
我想通过创建新列来将其转换为不同的结构,
desired_output
position,G,C,T,A,sampleid
1,0,0.002,0,0.998,name1
2, 0.96,0,0.04,0,name
3,0,0.93,0,0.07,name2
Run Code Online (Sandbox Code Playgroud)
我尝试过这种方法
per_position_full_nt_freq <- function(x){
df <- data.frame(A=0, C=0, G=0, T=0)
idx <- names(df) %in% x$alternative
df[,idx] <- x$mean_freq
idx2 <- names(df) %in% x$reference
df[,idx2] <- 1 - x$mean_freq
df$position <- x$position
df$sampleName <- x$sampleName
return(df)
}
desired_output_dataframe <- per_position_full_nt_freq(input_dataframe)
Run Code Online (Sandbox Code Playgroud)
我遇到了一个错误
In matrix(value, n, p) :
data length [8905] is not a sub-multiple or multiple of …Run Code Online (Sandbox Code Playgroud) 给定一串字符串,
array1 = ["abcdwillbegoneabcccc","cdefwilbegokkkabcdc"]
Run Code Online (Sandbox Code Playgroud)
和另一个由图案组成的字符串数组 ["abcd","beg[o|p]n","bcc","cdef","h*gxwy"]
任务是删除与任何模式字符串匹配的子字符串.例如,此案例的示例输出应为:
["willbegonea","wilbegokkk"]
Run Code Online (Sandbox Code Playgroud)
因为我们已经删除了匹配其中一个模式的子串(根据发生的位置,匹配的匹配或匹配).假设一个或两个匹配将始终发生在array1中每个字符串的开头或结尾处.
在ruby中对上述优雅解决方案的任何想法?
如果我有一个球列表,每个球都有颜色属性.我怎样才能干净地获得最常用颜色的球列表.
[m1,m2,m3,m4]
Run Code Online (Sandbox Code Playgroud)
说,
m1.color = blue
m2.color = blue
m3.color = red
m4.color = blue
Run Code Online (Sandbox Code Playgroud)
[m1,m2,m4] 是最常见颜色的球的列表
我的方法是:
[m1,m2,m3,m4].group_by{|ball| ball.color}.each do |samecolor|
my_items = samecolor.count
end
Run Code Online (Sandbox Code Playgroud)
其中count被定义为
class Array
def count
k =Hash.new(0)
self.each{|x|k[x]+=1}
k
end
end
Run Code Online (Sandbox Code Playgroud)
my_items将是一个相同颜色组的频率哈希值.我的实施可能是错误的,我觉得必须有一个更好,更聪明的方式.有什么想法吗?
什么是从D中的char []中删除空格的推荐方法,例如使用dmd 2.057,我有,
import std.stdio;
import std.string;
import std.algorithm;
char[] line;
int main(){
line = r"this is a line with spaces ";
line = removechars(line," ");
writeln(line);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,这将生成此错误:
Error: cannot implicitly convert expression ("this is a line with spaces ") of type string to char[]
Error: template std.string.removechars(S) if (isSomeString!(S)) does not match any function template declaration
Error: template std.string.removechars(S) if (isSomeString!(S)) cannot deduce template function from argument types !()(char[],string)
Run Code Online (Sandbox Code Playgroud)
在做一些谷歌搜索时,我发现类似的错误被报告为一个错误并且已经在2011年6月提交,但不确定它是指同一件事还是另一个问题.
一般来说,建议从字符串中删除某些字符并将前一个字符数组中的字符顺序排列的方法是什么?
在这种情况下返回 …
这里发生了什么?
irb(main):001:0> a= nil
=> nil
irb(main):002:0> b = nil
=> nil
irb(main):003:0> a
=> nil
irb(main):004:0> a+b
NoMethodError: undefined method `+' for nil:NilClass
from (irb):4
from :0
irb(main):005:0> if a.nil? or b.nil?; a,b=0;end;
irb(main):006:0* c = a+b
TypeError: nil can't be coerced into Fixnum
from (irb):6:in `+'
from (irb):6
from :0
irb(main):007:0>
Run Code Online (Sandbox Code Playgroud)
如何通过将nil转换为数字来安全地执行算术?
如何轻松解析具有此结构的文档
description
some line of text
another line of text
more lines of text
quality
3 47 88 4 4 4 4
text: type 1
stats some funny stats
description
some line of text2
another line of text2
more lines of text2
quality
1 2 4 6 7
text: type 1
stats some funny stats
.
.
.
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想要一个哈希结构数组,其中每个哈希表示文档的"部分",可能应该如下所示:
{:description =>"某行文字另一行文字更多行文字",:quality =>"3 47 88 4 4 4 4",:text => type 1,:stats =>"some funny stats" }
最近关于ruby解构的好文章将解构定义为将一组变量绑定到相应的一组值的能力,这些值通常可以将值绑定到单个变量,并给出了块解构的示例
triples = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
triples.each { |(first, second, third)| puts second } =>#[2, 5, 8]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们知道主数组中元素的数量,因此当我们提供参数first,second,third时,我们可以得到相应的结果.那么如果我们有一个数组的数组,其大小是在运行时确定的呢?
triples = [[1, 2, 3], [4, 5, 6], [7, 8, 9],...,[]]
Run Code Online (Sandbox Code Playgroud)
我们想要获得每个子阵列的第一个条目的元素?
triples.each { |(first, second, third,...,n)| puts first }
Run Code Online (Sandbox Code Playgroud)
(first, second, third,...,n)动态创建局部变量的最佳方法是什么?
所以我想实现一个哈希查找,用于将密码子翻译成D中的氨基酸.当我写的时候
int[string] codon_table = [
"ATG": 'M',
"TTT": 'F', "TTC": 'F', "TTA": 'L',
"TTG": 'L', "CTT": 'L', "CTC": 'L',
"CTA": 'L', "CTG": 'L', "ATT": 'I',
"ATC": 'I', "ATA": 'I', "GTT": 'V',
"GTC": 'V', "GTA": 'V', "GTG": 'V',
"TCT": 'S', "TCC": 'S', "TCA": 'S',
"TCG": 'S', "CCT": 'P', "CCC": 'P',
"CCA": 'P', "CCG": 'P', "ACT": 'T',
"ACC": 'T', "ACG": 'T', "GCT": 'A',
"GCC": 'A', "GCA": 'A', "GCG": 'A',
"TAT": 'Y', "TAC": 'Y', "TAA": '*',
"TAG": '*', "CAT": 'H', "CAC": 'H',
"CAA": …Run Code Online (Sandbox Code Playgroud)