简体   繁体   English

顺序重命名列数据帧

[英]Rename column data frame sequentially

I have a data frame like this 我有一个这样的数据框

   x           y          z

10             10         0      
00021          21         11    
022            22         1                                         
13610206     13610206     1     
13610207     13610207     1     
13610208     13610208     1     
13610209     13610209     1     
13610210     13610210     1 

where the second column is y = as.numeric(as.character(x)) and the third is z = diff(y). 其中第二列是y = as.numeric(as.character(x)),第三列是z = diff(y)。 What I would like to do is to add a column like this 我想做的就是添加一个这样的列

   x           y          z               xnew

10             10         0                10
00021          21         11               00021 
022            22         1                00021                                  
13610206     13610206     13610184         13610206
13610207     13610207     1                13610206
13610208     13610208     1                13610206
13610209     13610209     1                13610206
13610210     13610210     1                13610206

ie if z=1 xnew is equal to the previous x for which z is different from one. 即如果z = 1 xnew等于前一个x,且z不同于1。

You can use the function na.locf() ( l ast o bservation c arried f orward ) in package zoo : 可以使用函数na.locf() ASTöbservationÇarried˚Forward)在包zoo

First, import the data: 首先,导入数据:

dat <- read.table(text="
x           y          z
10             10         0      
00021          21         11    
022            22         1                                         
13610206     13610206     1     
13610207     13610207     1     
13610208     13610208     1     
13610209     13610209     1     
13610210     13610210     1 ", header=TRUE, colClasses=c("character", "numeric", "numeric"))

Next, the analysis 接下来,分析

library(zoo)

dat$z <- c(0, diff(dat$y))
dat$xnew <- na.locf(ifelse(dat$z==1, NA, dat$x))
dat

The results: 结果:

         x        y        z     xnew
1       10       10        0       10
2    00021       21       11    00021
3      022       22        1    00021
4 13610206 13610206 13610184 13610206
5 13610207 13610207        1 13610206
6 13610208 13610208        1 13610206
7 13610209 13610209        1 13610206
8 13610210 13610210        1 13610206

using apply : 使用apply

old <- NA
df$xnew <- apply(df, 1, function(row) {
              if (row['z'] != "1")
                old <<- row['x']
              old
            })
df
#          x        y        z     xnew
# 1       10       10        0       10
# 2    00021       21       11    00021
# 3      022       22        1    00021
# 4 13610206 13610206 13610184 13610206
# 5 13610207 13610207        1 13610206
# 6 13610208 13610208        1 13610206
# 7 13610209 13610209        1 13610206
# 8 13610210 13610210        1 13610206

So you have this: 所以你有这个:

df
#         x
#1       10
#2    00021
#3      022
#4 13610206
#5 13610207
#6 13610208
#7 13610209
#8 13610210

You can convert it to integer by df[1,]<- as.numeric(df[1,]) And then start to rest the values: 您可以通过df[1,]<- as.numeric(df[1,])将其转换为整数,然后开始df[1,]<- as.numeric(df[1,])值:

z<-0
for (i in 0:nrow(df)){
z<-c(z,df[i+1,]-df[i,])
df<-cbind(df, z)
}

But the Andrie solution is better, as it doesn't use the for loop, but I wanted to point how to manage the df of string to convert it to number to apply it 但是Andrie解决方案更好,因为它不使用for循环,但是我想指出如何管理string的df以将其转换为数字以应用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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