【问题标题】:apply function of R in python在python中应用R的函数
【发布时间】:2020-11-25 13:26:35
【问题描述】:

我在 R 中有一个有效的代码。但我想在python中重新做。我使用 R 来使用应用函数来计算次要等位基因频率。有人能告诉我这样的代码在 python 中的样子吗?我正在使用 pandas 读取 python 中的数据。

##R-code
###Reading file
var_freq <- read_delim("./cichlid_subset.frq", delim = "\t",
                       col_names = c("chr", "pos", "nalleles", "nchr", "a1", "a2"), skip = 1)

# find minor allele frequency
var_freq$maf <- var_freq %>% select(a1, a2) %>% apply(1, function(z) min(z))

我已经使用 pandas 阅读了该文件,但我正在努力处理第二部分。

###Python code
###Reading file
var_freq = pd.read_csv("./cichlid_subset.frq",sep='\t',header=None)
column_indices = [0,1,2,3,4,5]
new_names = ["chr", "pos", "nalleles", "nchr", "a1", "a2"]
old_names = df_snv_gnomad.columns[column_indices]

###Finding minor allele frequency

我们将不胜感激。

【问题讨论】:

标签: python r pandas


【解决方案1】:

用途:

# Read file
colnames = ["chr", "pos", "nalleles", "nchr", "a1", "a2"]
var_freq = pd.read_csv('./cichlid_subset.frq', sep='\t', header=None, skiprows=1, names=colnames)

# Get MAF
var_freq['maf'] = var_freq[['a1','a2']].min(axis=1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2021-07-13
    • 2015-05-21
    • 2017-07-11
    相关资源
    最近更新 更多