3

请帮忙!

我是 R 的新手,并试图进入它。我不断收到同样的警告:

    Warning messages:
 1: In if ((AttrSum[i, 1] == AllAttr[i:length(AllAttr), 1])) { :  the condition has length > 1 and only the first element will be used        

我有 3 个 csv 文件。1- AttrSum[90,2] 有 90 行,2 col....2-TC_RC [80,12]...3-AllAttr[70,20] 有 20 列和 70 行。我想检查 AttrSum[,1] 的第一列是否等于 TC_RC [,12] 的第 12 列,然后将 AllAttr[,19] 的第 19 列除以 AttrSum[,2] 的第 2 列 wrt 适当的第 1 列(存在于 TC_RC [,12]=RC_ind) 中。我正在这样做:

 AttrSum <-read.csv()
AllAttr <- read.csv()

RC_sum <-AttrSum [AttrSum [,1]%in%TC_RC[,12], col_ind]# values
RC_ind <- AttrSum [(AttrSum [,1] %in%TC_RC[,12]), 1]#names


len_attrSum <- length (AttrSum)
CV <- c()

for (i in 1:len_attrSum){


if (all(RC_ind [i] == AllAttr[i:length(AllAttr),1])){

CV[i] <- (RC_sum[i]/AllAttr[,19])

}
}

很抱歉这样一个基本问题,但我被困在这里。我知道我的循环有问题,但看不到它是什么。我查看了 R 简介,但仍然无法理解。

提前致谢

PS:1-AttrSum[90,2] 文件。

Case    x
2   1.784309
3   2.836969
4   0.791783
5   1.812687
8   0.385067
.......
90  0.771613


2-TC_RC[80,12] file. 12 columns,80 rows.
10  41.166667   1   0.364352    47  0.944911    49       26.833333  26.833333   1   0.324537    49

100 40.625          0   0.112847    55  0.953485    107 33.625  42.25   0   0.117361    109

101 29.75          0    0.082639    111 0.917909    107 12.625  29.75   0   0.082639    111
  1. AllAttr [90,19] 文件

    Case    V16 V15 V14 V8  V9  V9.1    V10 V11 V12 V13 V1  V2  V3  V4  V5  V6  Vl7 VB
      2 0.577967    0.023869    0.021571    0.481754    0.61584 0   0   0   0   0   0.024057    0.039251    0   0   0   0   0   4
      3 0.327011    0.095338    0.025591    0.785795    0.511902    0.516165    0   0   0   0   0.033882    0.028056    0.513229    0   0   0   0   4
    
4

2 回答 2

5

R 中的if语句未向量化。所以如果你运行代码if(1:2 == 1:2),你会得到同样的错误。相反,将您的比较包含在allor中anyif(all(1:2 == 1:2))

于 2013-08-08T13:50:59.587 回答
1

这是我的解决方案(未测试,因为您没有提供可重现的样本数据):

AttrSum$new[AttrSum[,1] %in% TC_RC [,12]]<-
AllAttr[,19][AttrSum[,1] %in% TC_RC [,12]]/AttrSum[,2][AttrSum[,1] %in% TC_RC [,12]]

注意:我假设您要检查 TC-RC 第 12 列的所有行上的 AttrSum 第一列的每一行,然后将结果商(右侧)输出为 AttrSum 的新列。

更新了 TC_RC 数据的一些变化(如 2,5 和 11)以显示上述代码有效

AttrSum<-structure(list(col1 = 1:5, col2 = c(1.784309, 2.836969, 0.791783, 
1.812687, 0.385067)), .Names = c("col1", "col2"), row.names = c(NA, 
-5L), class = "data.frame")
> AttrSum
  col1     col2
1    1 1.784309
2    2 2.836969
3    3 0.791783
4    4 1.812687
5    5 0.385067
AllAttr<-structure(list(col111 = 2:3, col112 = c(0.577967, 0.327011), 
    col113 = c(0.023869, 0.095338), col114 = c(0.021571, 0.025591
    ), col115 = c(0.481754, 0.785795), col116 = c(0.61584, 0.511902
    ), col117 = c(0, 0.516165), col118 = c(0L, 0L), col119 = c(0L, 
    0L), col120 = c(0L, 0L), col121 = c(0L, 0L), col122 = c(0.024057, 
    0.033882), col123 = c(0.039251, 0.028056), col124 = c(0, 
    0.513229), col125 = c(0L, 0L), col126 = c(0L, 0L), col127 = c(0L, 
    0L), col128 = c(0L, 0L), col129 = c(4L, 4L)), .Names = c("col111", 
"col112", "col113", "col114", "col115", "col116", "col117", "col118", 
"col119", "col120", "col121", "col122", "col123", "col124", "col125", 
"col126", "col127", "col128", "col129"), class = "data.frame", row.names = c(NA, 
-2L))
>AllAttr
  col111   col112   col113   col114   col115   col116   col117 col118 col119 col120 col121   col122   col123   col124 col125
1      2 0.577967 0.023869 0.021571 0.481754 0.615840 0.000000      0      0      0      0 0.024057 0.039251 0.000000      0
2      3 0.327011 0.095338 0.025591 0.785795 0.511902 0.516165      0      0      0      0 0.033882 0.028056 0.513229      0
  col126 col127 col128 col129
1      0      0      0      4
2      0      0      0      4
TC_RC<-structure(list(col11 = c(10L, 100L, 101L), col12 = c(41.166667, 
40.625, 29.75), col13 = c(1L, 0L, 0L), col14 = c(0.364352, 0.112847, 
0.082639), col15 = c(47L, 55L, 111L), col16 = c(0.944911, 0.953485, 
0.917909), col17 = c(49L, 107L, 107L), col18 = c(26.833333, 33.625, 
12.625), col19 = c(26.833333, 42.25, 29.75), col20 = c(1L, 0L, 
0L), col21 = c(0.324537, 0.117361, 0.082639), col22 = c(2L, 5L, 
111L)), .Names = c("col11", "col12", "col13", "col14", "col15", 
"col16", "col17", "col18", "col19", "col20", "col21", "col22"
), class = "data.frame", row.names = c(NA, -3L))
> TC_RC
  col11    col12 col13    col14 col15    col16 col17    col18    col19 col20    col21 col22
1    10 41.16667     1 0.364352    47 0.944911    49 26.83333 26.83333     1 0.324537     2
2   100 40.62500     0 0.112847    55 0.953485   107 33.62500 42.25000     0 0.117361     5
3   101 29.75000     0 0.082639   111 0.917909   107 12.62500 29.75000     0 0.082639   111

> AttrSum[,1] %in% TC_RC [,12]
[1] FALSE  TRUE FALSE FALSE  TRUE

AttrSum$new[AttrSum[,1] %in% TC_RC [,12]]<-
    AllAttr[,19][AttrSum[,1] %in% TC_RC [,12]]/AttrSum[,2][AttrSum[,1] %in% TC_RC [,12]]

> AttrSum
  col1     col2      new
1    1 1.784309       NA
2    2 2.836969 1.409955
3    3 0.791783       NA
4    4 1.812687       NA
5    5 0.385067       NA

如果您不想将其附加到 AttrSum ,则可以执行以下操作:

hello<-c()
hello[AttrSum[,1] %in% TC_RC [,12]]<-
        AllAttr[,19][AttrSum[,1] %in% TC_RC [,12]]/AttrSum[,2][AttrSum[,1] %in% TC_RC [,12]]
    > hello
[1]       NA 1.409955       NA       NA       NA
于 2013-08-08T15:05:39.767 回答