简体   繁体   中英

Use of ddply instead of a loop - subtracting for particular categories

I have a dataset with 2 numeric columns.

Example dataset:

X = c(-1:-20)
Y=c(11:30)
df=as.data.frame(cbind(X,Y))

My dataset looks like:

     X  Y
1   -1 11
2   -2 12
3   -3 13
4   -4 14
5   -5 15
6   -6 16
7   -7 17
8   -8 18
9   -9 19
10 -10 20
11 -11 21
12 -12 22
13 -13 23
14 -14 24
15 -15 25
16 -16 26
17 -17 27
18 -18 28
19 -19 29
20 -20 30

I'm using a loop that allows me to subtract a 100 to value below -10.

for (i in 1:length(df[,1]))
{
  if ((df$X[i]< c(-10.0)) == T)
  {df$X[i] = df$X[i] - 100}else
  {}
}

My "real" dataset contains 300 000 lines and the loop is really time consuming. That's why I've been trying to find an apply function that does the job.

library(plyr)
TAB1=ddply(df,.(X),function (x) x[(df$x)< c(-10.0)]-100)

But it's not working at all.

Thank your for any help.

Don't use ddply for this task. You don't need it. The operations are vectorized

index <- df$X < -10
df$X[index] <- df$X[index] - 100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM