在R中的每一行上执行if语句

Bil*_*ill 4 syntax r function apply

我在一个csv文件中读到R,看起来像这样:

3,3
3,2
3,3
3,3
3,3
3,3
2,3
1,2
2,2
3,3
Run Code Online (Sandbox Code Playgroud)

我想为我的数据可能的9种独特可能性中的每一种分配一个数字(3和3是9,3和2是8,2和3是6等).我一直在尝试设计一个嵌套的if语句,它将评估每一行,在第三列中分配一个数字,并为数据集中的每一行执行此操作.我相信这可以通过apply函数来完成,但是我无法在apply函数中使用if语句.这两列都有可能的值1,2或3.这是我的代码到目前为止,只是尝试分配9到3/3列,0到其他所有:

#RScript for haplotype analysis

#remove(list=ls())
options(stringsAsFactors=FALSE)
setwd("C:/Documents and Settings/ColumbiaPC/Desktop")

#read in comma-delimited, ID-matched genotype data
OXT <- read.csv("OXTRhaplotype.csv")
colnames(OXT)<- c("OXT1","OXT2")

OXT$HAP <- apply(OXT, 1, function(x) if(x[1]=="3"&&x[2]=="3")x[3]=="9" else 0))
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

And*_*rie 11

您可以使用矩阵和标准R子集来解决您描述的问题,而无需任何if语句

m <- matrix(1:9, nrow=3, byrow=TRUE)
m

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

这意味着您可以使用矩阵子集来索引m:

m[3, 2]
[1] 8

m[3,3]
[1] 9

m[2,3]
[1] 6
Run Code Online (Sandbox Code Playgroud)

现在您可以将其应用于您的数据:

df <- structure(list(V1 = c(3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 2L, 3L), 
        V2 = c(3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L)), .Names = c("V1", 
        "V2"), class = "data.frame", row.names = c(NA, -10L))

#df$m <- sapply(seq_len(nrow(df)), function(i)m[df$V1[i], df$V2[i]])
df$m <- m[as.matrix(df)]  # Use matrix subsetting, suggested by @Aaron
df

   V1 V2 m
1   3  3 9
2   3  2 8
3   3  3 9
4   3  3 9
5   3  3 9
6   3  3 9
7   2  3 6
8   1  2 2
9   2  2 5
10  3  3 9
Run Code Online (Sandbox Code Playgroud)

  • 更好的是,用矩阵子集替换`sapply`:尝试`m [as.matrix(df)]`. (3认同)

chl*_*chl 5

不幸的是,我来晚了,解决方案类似于@Andrie的解决方案,如下所示:

dat <- matrix(c(3,3,3,2,3,3,3,3,3,3,3,3,2,3,1,2,2,2,3,3), 
              nr=10, byrow=TRUE) 
# here is our lookup table for genotypes
pat <- matrix(1:9, nr=3, byrow=T, dimnames=list(1:3,1:3))
Run Code Online (Sandbox Code Playgroud)

然后

> pat[dat]
 [1] 9 8 9 9 9 9 6 2 5 9
Run Code Online (Sandbox Code Playgroud)

给你你想要的.

但是,我想说您可能会发现更容易使用专用包进行遗传研究,例如在CRAN(例如genetics,gap或者SNPassoc,仅举几例)或Bioconductor上发现的包,因为它们包括用于转化/重新编码基因型数据的设施和使用单倍型.

以下是我对上述说法的一个例子:

> library(genetics)
> geno1 <- as.genotype.allele.count(dat[,1]-1)
> geno2 <- as.genotype.allele.count(dat[,2]-1)
> table(geno1, geno2)
     geno2
geno1 A/A A/B
  A/A   6   1
  A/B   1   1
  B/B   0   1
Run Code Online (Sandbox Code Playgroud)


jth*_*zel 5

Andrie已经通过更好地解决您的问题来回答您的问题.但是我想提到的原始代码中有一些错误.

首先,&是不一样的&&.了解?'&'更多.我相信你想&在你的例子中使用.

其次,==用于相等性测试,最初在您的示例中使用正确.它不用于赋值,在分配"9"时错误地使用它x[3].分配由<-内部或外部功能处理.见?'=='?'<-'更多.

第三,x[3]apply()函数内赋值是没有意义的. apply()只需返回一个数组.它不会修改OXT对象.以下是原始方法的外观示例.但是,Andrie的方法对你来说可能更好.

OXT <- read.table(textConnection(
    "3 3
    3 2
    3 3
    3 3
    3 3
    3 3
    2 3
    1 2
    2 2
    3 3"))
colnames(OXT)<- c("OXT1","OXT2")

OXT$HAP <- apply(OXT, 1, function(x)
    {
        if(x[1] == 3 & x[2] == 3) result <- 9
        else if(x[1] == 3 & x[2] == 2) result <- 8
        else if(x[1] == 3 & x[2] == 1) result <- 7
        else result <- 0
        return(result)
    })
Run Code Online (Sandbox Code Playgroud)