I am new to R and i've been stuck on this. I have a data set below wherein I created a new array list variable called 'amountOfTxn_array' that contains three numeric values in sequential order. These are amounts of transactions taken from Jan to Mar. My objective is to create new variables from this array list that iterate each data elements in the 'amountOfTxn_array'.
> head(myData_05_Array)
Index accountID amountOfTxn_array
1:00 8887 c(36.44, 75.00,185.24)
2:00 13462 c(639.45,656.10,237.00)
3:00 47249 c(0, 24, 2012)
4:00 49528 c(1189.20,2326.26,1695.89)
5:00 57201 c(24.67, 0.00, 0.00)
6:00 57206 c(0.00, 661.98,2957.68)
str(myData_05_Array) Classes ‘data.table’ and 'data.frame': 3176 obs. of 4 variables: $ accountID : int 8887 13462 47249 49528 57201 57206 58522 79073 80465 81032 ... $ amountOfTxn_200501: num 36.4 639.5 0 1189.2 24.7 ... $ amountOfTxn_200502: num 75 656 24 2326 0 ... $ amountOfTxn_200503: num 185 237 2012 1696 0 ... $ amountOfTxn_array :List of 3176
Also, an example code for creating a new variable is provided below wherein I would like to tag 1 if a value in the array is greater than 100 and 0 else. When I ran the example code, I am getting "Error: (list) object cannot be coerced to type ‘double’ error. May I ask for a solution for this. I would highly appreciate any response. Thanks!
> for(i in 1:3)
+ {
+ if(myData_05_Array$amountOfTxn_array[i] > 100){
+ myData_05_Array$testArray[i] <- 1
+ }
+ else{
+ myData_05_Array$testArray[i] <- 0
+ }
+ }
Error: (list) object cannot be coerced to type 'double'
What I am expecting as the output is as follows: amountOfTxn_testArray c(0, 0, 1) c(1, 1, 1) c(0, 0, 0) c(1, 1, 1) c(0, 0, 0) c(0, 1, 1)
myData_05_Array$amountOfTxn_array[i]
you take the i 'th row/entry ofamoutOfTxn_array
which is a vector and in this situation, you compare a vector with100
which throws an error. Maybe you split this column up to 3 and compare every element – wolf_wuestr(myData_05_Array)
? – Janna Maasany()
to check a condition within, e.g., avector
:any(c(1:10) >5)
so you don't have to use a loop. – Janna Maas