I want to change the specific components of R's two-dimensional array.

Asked 2 years ago, Updated 2 years ago, 33 views

I'd like to make all values that appear more than 10 times from 2D array a random. How should I describe them?I want to do it without looping.
For writing using the for statement,

for(time in unique(a)){
  if(length(a[a==time])>=10){
    a[a==time]<-0
  }
}

I think I can write like that, but I don't know how to make it happen without using a loop.The work we're doing now involves a large number of two-dimensional arrays, so we're looking for ways to achieve this without looping as much as possible.
If you are familiar with the description of R, please take care of it.

r

2022-09-30 13:46

1 Answers

The following is how to use the table function:

f<-as.data.frame(table(a))
a [a% in %f [f$Freq>=10,]$a] <-0

You can use the table function to obtain the frequency at which unique elements appear.


2022-09-30 13:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.