在 R 中重新缩放 data.frame 中的选定数据

rescale selected data in data.frame in R

我无法理解这个: 假设我有一个数据框:

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

我只想使用函数

重新调整具有特定 ID 和位置的计数
rescale(data,c(1,10)) #library(scales)

我想将结果写入数据中的额外列 y。

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))

这会重新调整 data$diff 中的所有值,而不仅仅是我想根据条件调用的值。

 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

有什么建议可以帮助我获得理想的结果吗?

我假设当您说您不想重新缩放 data$diff 中的所有值时,您的意思是您只想重新缩放满足您的 ifelse() 的特定行. IE。您想要将 data$diff 的子集而不是整列传递给 rescale。为此,您可以这样做:

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