Search code examples
rloopsrescale

rescale selected data in data.frame in R


I can't get my head around this: Lets say I have a data frame:

ID<-c("a","a","b","b","c","c","c","d","d")
count_1<-runif(9)
count_2<-runif(9)
diff<-count_1-count_2
pos<-c(1,1,1,2,2,2,3,3,3)
data<-data.frame(ID,count_1,count_2,diff,pos)
head(data)

  ID   count_1   count_2        diff pos
1  a 0.8822875 0.9180848 -0.03579732   1
2  a 0.3641642 0.4097200 -0.04555586   1
3  b 0.2235055 0.9074667 -0.68396115   1
4  b 0.7228688 0.1091750  0.61369374   2
5  c 0.5627312 0.3356446  0.22708664   2
6  c 0.2036120 0.6002063 -0.39659429   2

I would like to rescale only the counts with a certain ID and position with the function

rescale(data,c(1,10)) #library(scales)

I would like to write the results to an extra column y in data.

data$y<-ifelse(data$pos==1 & data$ID=="a",rescale(data$diff,c(1,10)),
               ifelse(data$position==3 & data$ID=="c",rescale(data$diff,c(1,10)),NA))

This rescales all the the values in data$diff and not only the ones I would like to call by my conditions.

 ID   count_1   count_2        diff pos        y
1  a 0.8822875 0.9180848 -0.03579732   1 4.876081
2  a 0.3641642 0.4097200 -0.04555586   1 4.817724
3  b 0.2235055 0.9074667 -0.68396115   1       NA
4  b 0.7228688 0.1091750  0.61369374   2       NA
5  c 0.5627312 0.3356446  0.22708664   2       NA
6  c 0.2036120 0.6002063 -0.39659429   2       NA

Any suggestions how I can manage to get the desired result?


Solution

  • I'm assuming that when you say that you don't want to rescale all the values in data$diff, you mean that you only want to rescale the specific rows that satisfy your ifelse(). I.e. you want to pass a subset of data$diff to rescale rather than the whole column. To do this, you could do:

    set.seed(1) #For the earlier data creation
    
    rows <- (data$pos==1 & data$ID=="a") | (data$pos==3 & data$ID=="c")
    data[rows, "y"] <- rescale(data[rows,"diff"], c(1,10))
    
    data
    #  ID   count_1    count_2        diff pos        y
    #1  a 0.2655087 0.06178627  0.20372239   1  2.20415
    #2  a 0.3721239 0.20597457  0.16614932   1  1.00000
    #3  b 0.5728534 0.17655675  0.39629661   1       NA
    #4  b 0.9082078 0.68702285  0.22118494   2       NA
    #5  c 0.2016819 0.38410372 -0.18242179   2       NA
    #6  c 0.8983897 0.76984142  0.12854826   2       NA
    #7  c 0.9446753 0.49769924  0.44697603   3 10.00000
    #8  d 0.6607978 0.71761851 -0.05682072   3       NA
    #9  d 0.6291140 0.99190609 -0.36279205   3       NA