我有这个数据框(但非常大,包括许多行在name变量保存值yes没有和)
df = data.frame(name = c('a','b','c',NA,NA,'yes','no',NA,NA,'f','g','h'),
Freq = c(10,20,70,NA,NA,40,60,NA,NA,80,10,10) )
# output
name Freq
1 a 10
2 b 20
3 c 70
4 <NA> NA
5 <NA> NA
6 yes 40
7 no 60
8 <NA> NA
9 <NA> NA
10 f 80
11 g 10
12 h 10
对于每两个包含的连续行,yes没有和在name列中,我希望它们被翻转成如下格式:
# output
name Freq
1 a 10
2 b 20
3 c 70
4 <NA> NA
5 <NA> NA
6 no 60
7 yes 40
8 <NA> NA
9 <NA> NA
10 f 80
11 g 10
12 h 10
感谢您的帮助
一种方法如下:
- 检查哪些行包含相邻的yes/no值
- 创建一个索引向量,交换这些行数
- 使用此向量重新排序数据
df = data.frame(name = c('a','b','c',NA,NA,'yes','no',NA,NA,'f','g','h','no','yes'),
Freq = c(10,20,70,NA,NA,40,60,NA,NA,80,10,10,20,20) )
# first find the adjacent rows containing 'yes' and 'no'
# i.e. row contains yes/no & next row contains yes/no & they are not the same
rows <- which(
df$name %in% c('yes','no') &
c(df[-1, 'name'],NA) %in% c('yes','no') &
df$name != c(df[-1,'name'],NA)
)
# next, swap the row with the next row for each of these by creating an index
# variable with these numbers swapped
index <- 1:nrow(df)
index[rows]<-rows+1
index[rows+1] <- rows
df <- df[index,]
print(df)
name Freq
1 a 10
2 b 20
3 c 70
4 <NA> NA
5 <NA> NA
7 no 60
6 yes 40
8 <NA> NA
9 <NA> NA
10 f 80
11 g 10
12 h 10