0
votes

I am working with series of data, say nrow=500 & ncol=300.

this is only for example:

 V211      V212      V213      V214      V215      V216
[1,] 2.6088731 2.5531749 2.6017147 2.6124385 2.7156729 2.7897892
[2,] 1.4816736 0.9634576 0.9346004 0.9999343 -1.1096868 1.0848002
[3,] 1.1547304 -0.9052018 0.9036510 0.8504258 0.8382223 0.9237510
[4,] 1.0049771 0.9032115 0.8538077 -0.7911187 0.8244491 0.9172450
[5,] 0.9888954 0.8589149 0.7850069 0.6574331 0.7917890 0.7667152
[6,] -0.9577423 0.7662278 0.7767129 0.5639352 0.7559162 0.7118414

each column has positive and negative numbers. What i wanna do is to filter positive numbers of each column and write those numbers into new matrix.

What i have done:

  1. read .csv data as matrix "data"
  2. create new matrix "new" matrix(0,500,300)
  3. Using loop

    for (i in 1:300)
    
       {a=data[which(data[,i]>0)]
    
        new[,i]=print(a)}
    

Then problems came:

it shows only positions of the data instead of the data itself (example):

    [,238] [,239] [,240] [,241] [,242] [,243] [,244] [,245] [,246]
[1,]      1      1      1      1      1      1      1      1      1
[2,]      2      2      2      2      2      2      2      2      2
[3,]      3      3      3      3      3      3      3      3      3
[4,]      4      4      4      4      4      4      4      4      4
[5,]      5      5      5      5      5      5      5      5      5
[6,]      6      6      6      6      6      6      6      6      6

i have been searching another possible solutions but what I've found mostly data with header on each column. Since i have a lot of series, it needs a long time to name them one by one.

Please leave me some advise! thanks

1
The result can't be a matrix since the number of positive entries is not equal in all columns. Also, a for loop is not the best tool here. - Roland
Or do you just want to set the negative values to NA? - Roland
set the negative values to NA is a good idea. How could i do that? thanks @Roland - beboo23

1 Answers

0
votes

A reproducible example:

set.seed(42)
m <- matrix(rnorm(16), 4)
#           [,1]        [,2]       [,3]       [,4]
#[1,]  1.3709584  0.40426832  2.0184237 -1.3888607
#[2,] -0.5646982 -0.10612452 -0.0627141 -0.2787888
#[3,]  0.3631284  1.51152200  1.3048697 -0.1333213
#[4,]  0.6328626 -0.09465904  2.2866454  0.6359504

#copy the matrix
mpos <- m
#assignment to subset
mpos[mpos < 0] <- NA
#          [,1]      [,2]     [,3]      [,4]
#[1,] 1.3709584 0.4042683 2.018424        NA
#[2,]        NA        NA       NA        NA
#[3,] 0.3631284 1.5115220 1.304870        NA
#[4,] 0.6328626        NA 2.286645 0.6359504

You should study help("[").