0

I have a small problem. I know using data.frame and loop is not that good and my function doesn't update the columns I want to change. Any idea on how to make this function work? Thanks in advance

Func = function(data,k){

  result_freq = data.frame(apply(data,2,function(x) length(unique(x))))

  for (w in 1:nrow(result_freq)){

    if (result_freq[w,]>(nrow(data)/3)) { 
      data[,w] = as.integer(paste(round( data[,w]/ 1e3)))
    } 
  }
  return(data)
}

Func(data,5)

My original data is something like that, and i want this column to be cut by 1K and updated in the original data.frame

1300 => 1
1421 => 1

34535 => 34
  • Your function argument is `k` and you also use `k` in loop - I don't understand what you want to achieve. – pogibas Apr 13 '17 at 15:47
  • Yeah sorry forgot to change but it does not make any different on the result – Nina display Apr 13 '17 at 15:49
  • Please provide [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) when you're asking a question. Also, It's difficult to troubleshoot when we have no idea what you want the function to accomplish. Providing an expected result will help greatly in the communication. – Adam Quek Apr 14 '17 at 06:17

0 Answers0