用vector来动态地替换数据框中的行 [英] Dynamically replace row in dataframe with vector
问题描述
我有一个data.frame例如:
d< - read.table(text ='V1 V2 V3 V4 V5 V6 V7
1 1 a 2 3 4 9 6
2 1 b 2 2 4 5 NA
3 1 c 1 3 4 5 8
4 1 d 1 2 3 6 9
5 2 a 1 2 3 4 5
6 2 b 1 4 5 6 7
7 2 c 1 2 3 5 8
8 2 d 2 3 6 7 9 ',header = TRUE)
现在我想要一行,例如第一行(1a )和:
-
从该行获取最小和最大值。在这种情况下,min = 2和max = 9(注意,两个之间缺少值,例如,该行中没有5,7或8)。
-
现在我想用所有缺少的值替换该行并扩展它(该行将比所有其他行更长,因为它将从2到9 2,3,4,5,6,7,8,9)。然后,整个数据框应该被NA列自动扩展,而不是我所替换的其他行。
我尝试了一种矢量方法,但我几乎陷入困境,这是我迄今为止所做的:
vec.one_< - d [1,]
vec.one< - as.vector(vec.one_ [3:length .one_)])
min.one< - min(vec.one,na.rm = T)
max.one< - max(vec.one,na.rm = T)
new.one< - as.numeric(vector(,length(vec.one)))
for(i in 1:length(new.one)){
new.one [我试图把这行作为向量来提取,以便操作。它然后把它放回来。我不知道是否有效。
这应该是data.frame一旦第一行被替换,并且整个data.frame自动与NAs扩展:
d < - read.table(text ='V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 1 a 2 3 4 5 6 7 8 9
2 1 b 2 2 4 5 NA NA NA NA
3 1 c 1 3 4 5 8 NA NA NA
4 1 d 1 2 3 6 9 NA NA NA
5 2 a 1 2 3 4 5 NA NA NA
6 2 b 1 4 5 6 7 NA NA NA
7 2 c 1 2 3 5 8 NA NA NA
8 2 d 2 3 6 7 9 NA NA NA',header = TRUE)
解决方案第一步是计算要更改的行的新值:
row.to.change< - 1
(new.row< - seq(min(d [row.to.change,c(-1,-2)],na。 rm = TRUE),max(d [row.to.change,c(-1,-2)],na.rm = TRUE)))
#[1] 2 3 4 5 6 7 8 9
然后我将数据框延伸到正确的大小:
(num.add< - length(new.row) - ncol(d)+ 2)
#[1] 3
if (num.add> 0){
d < - cbind(d,replicate(num.add,rep(NA,nrow(d))))
} else if(num.add< = 0){
new.row< - c(new.row,rep(NA,-num.add))
}
最后,我将替换感兴趣的行中的元素:
d [row.to .change,c(-1,-2)]< - new.row
d
#V1 V2 V3 V4 V5 V6 V7 1 2 3
#1 1 a 2 3 4 5 6 7 8 9
#2 1 b 2 2 4 5 NA NA NA NA
#3 1 c 1 3 4 5 8 NA NA NA
#4 1 d 1 2 3 6 9 NA NA NA
#5 2 a 1 2 3 4 5 NA NA NA
#6 2 b 1 4 5 6 7 NA NA NA
#7 2 c 1 2 3 5 8 NA NA NA
#8 2 d 2 3 6 7 9 NA NA NA
I have a data.frame for example:
d <- read.table(text=' V1 V2 V3 V4 V5 V6 V7
1 1 a 2 3 4 9 6
2 1 b 2 2 4 5 NA
3 1 c 1 3 4 5 8
4 1 d 1 2 3 6 9
5 2 a 1 2 3 4 5
6 2 b 1 4 5 6 7
7 2 c 1 2 3 5 8
8 2 d 2 3 6 7 9', header=TRUE)
Now I want to take one row, for example the first one (1a) and:
Get the min and max value from that row. In this case min=2 and max=9 (note there are missing values in between for example there is no 5, 7, or 8 in that row).
Now I want to replace that row with all missing values and extend it (the row will be longer than all others as it will go from 2 until 9 (2,3,4,5,6,7,8,9). The whole data.frame should then be automatically extended by NA columns for the other rows that are not as long as the one I replaced.
I tried a vector approach but I am pretty much stuck. This is what I did so far:
vec.one_ <- d[1,]
vec.one <- as.vector(vec.one_[3:length(vec.one_)])
min.one <- min(vec.one, na.rm=T)
max.one <- max(vec.one, na.rm=T)
new.one <- as.numeric(vector(,length(vec.one)))
for(i in 1:length(new.one)){
new.one[i] <- NA
}
I tried to extract the row as vector to manipulate it and then put it back in. I am not sure that works.
This should be the data.frame once the first row got replaced and the whole data.frame automatically extended with NAs:
d <- read.table(text=' V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 1 a 2 3 4 5 6 7 8 9
2 1 b 2 2 4 5 NA NA NA NA
3 1 c 1 3 4 5 8 NA NA NA
4 1 d 1 2 3 6 9 NA NA NA
5 2 a 1 2 3 4 5 NA NA NA
6 2 b 1 4 5 6 7 NA NA NA
7 2 c 1 2 3 5 8 NA NA NA
8 2 d 2 3 6 7 9 NA NA NA', header=TRUE)
解决方案 The first step is to compute the new values for the row you want to change:
row.to.change <- 1
(new.row <- seq(min(d[row.to.change,c(-1, -2)], na.rm=TRUE), max(d[row.to.change,c(-1,-2)], na.rm=TRUE)))
# [1] 2 3 4 5 6 7 8 9
Then I would extend out the data frame to the correct size:
(num.add <- length(new.row) - ncol(d) + 2)
# [1] 3
if (num.add > 0) {
d <- cbind(d, replicate(num.add, rep(NA, nrow(d))))
} else if (num.add <= 0) {
new.row <- c(new.row, rep(NA, -num.add))
}
Finally, I would replace the elements in the row of interest:
d[row.to.change,c(-1, -2)] <- new.row
d
# V1 V2 V3 V4 V5 V6 V7 1 2 3
# 1 1 a 2 3 4 5 6 7 8 9
# 2 1 b 2 2 4 5 NA NA NA NA
# 3 1 c 1 3 4 5 8 NA NA NA
# 4 1 d 1 2 3 6 9 NA NA NA
# 5 2 a 1 2 3 4 5 NA NA NA
# 6 2 b 1 4 5 6 7 NA NA NA
# 7 2 c 1 2 3 5 8 NA NA NA
# 8 2 d 2 3 6 7 9 NA NA NA
这篇关于用vector来动态地替换数据框中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!