3

please help!

I'm new to R and trying to get into it. I keep getting same warning:

    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        

I have 3 csv files. 1- AttrSum[90,2] has 90 rows,2 col....2-TC_RC [80,12]...3-AllAttr[70,20] has 20 columns and 70 rows. I want to check if first column of AttrSum[,1] is equal to 12th column of TC_RC [,12], then divide 19th column of AllAttr[,19] to the 2nd column of AttrSum[,2] w.r.t appropriate 1st columns(that are present in TC_RC [,12]=RC_ind). I'm doing this:

 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])

}
}

Sorry for such a basic question but i'm stuck here. I understand that i have problem with my loop but cannot see what is it. I looked at Introduction to R but still cannot get it.

Thanks in advance

PS: 1-AttrSum[90,2] file.

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] file

    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
    
mil
  • 301
  • 1
  • 2
  • 15

2 Answers2

5

the if statement in R is not vectorized. So if you run the code if(1:2 == 1:2) you will get the same error. Instead, wrap your comparison in all or any: if(all(1:2 == 1:2))

Justin
  • 42,475
  • 9
  • 93
  • 111
  • Thanks a lot for your help. I Will consider this for future loops. I have fixed it and no warning appears but i get CV. NULL..I have also edited my question because I need to compare ot the 3rd file but it still gave NULL with previous version too. – mil Aug 08 '13 at 14:24
  • without knowing what your data looks like, we cannot help much more. if you want to supply some of your data, using `dput` then we can reproduce your problem. Otherwise, its all guessing. – Justin Aug 08 '13 at 15:36
  • However, I would instead suggest asking a new question since your initial question was answered. Often the process of putting the question together and making it [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) leads to finding your own answer! – Justin Aug 08 '13 at 15:47
1

Here is my solution (not tested since you didn't provide reproducible sample data):

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

Note: I am assuming you want to check each row of first column of AttrSum on all rows of 12th column of TC-RC and then output the resulting quotient (of the right hand side) as new column of AttrSum.

Updated with some changes in data of TC_RC (as 2,5, and 11) to show that the above code works

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

If you don't want to attach it to AttrSum , then you can do as follows:

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
Metrics
  • 15,172
  • 7
  • 54
  • 83
  • Thank you so much! This is what I need. Could you explain the left handside part please-'as new column of AttrSum'?What does it mean? I assumed it will result in new column (with results of division) in the AttrSum file but there is no such. – mil Aug 08 '13 at 15:32
  • Yes, you are right. If you run with your real data, those not matching your condition should give `NA` values. Note you can always extract this column and remove `NA` values if you want. – Metrics Aug 08 '13 at 15:36
  • That's the problem. There is no new column with the results in the AttrSum or any of the files. – mil Aug 08 '13 at 16:05
  • Ok, I got it. Can you please provide the sample data, so that I can test the code? – Metrics Aug 08 '13 at 16:15
  • Yes I have already edited my question putting files with sample data. Thank you! – mil Aug 08 '13 at 16:19
  • Thanks. Please `dput` the data. – Metrics Aug 08 '13 at 16:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35088/discussion-between-mil-and-metrics) – mil Aug 08 '13 at 16:34
  • Sorry,I am not available for that:I will try to post the solution for you data and you can check on that. – Metrics Aug 08 '13 at 16:37
  • Hi! sorry but I'm trying to match col 12 with col Case 2 3 4 5 8 because I'm comparing TC_RC [,12] with AttrSum [,1] which == 'Case' – mil Aug 08 '13 at 16:41
  • I have made some changes in the data to confirm that the code works. See the updated solution. – Metrics Aug 08 '13 at 16:57
  • Thanks I can't see updated version. Solution didn't update..it's the same old one – mil Aug 08 '13 at 17:08