我有类似以下内容的大量数据:

week_0<-c(5,0,1,0,8,1)
week_4<-c(1,0,1,0,1,1)
week_8<-c(1,0,6,0,0,0)
week_9<-c(2,4,1,7,8,1)
week_10<-c(2,4,1,7,8,1)
Participant<-c("Lion","Cat","Dog","Snake","Tiger","Mouse")
test_data<-data.frame(Participant,week_0,week_4,week_8,week_9,week_10)

 > test_data
 Participant week_0 week_4 week_8 week_9 week_10
1        Lion      5      1      1      2       2
2         Cat      0      0      0      4       4
3         Dog      1      1      6      1       1
4       Snake      0      0      0      7       7
5       Tiger      8      1      0      8       8
6       Mouse      1      1      0      1       1

我想填一下列名数字之间的空白处.我想要的最终结果是:

test_data
      Participant week_0 week_1 week_2 week_3 week_4 week_5 week_6 week_7 week_8 week_9 week_10
1        Lion      5      5      5      5      1      1      1      1      1      2       2
2         Cat      0      0      0      0      0      0      0      0      0      4       4
3         Dog      1      1      1      1      1      1      1      1      6      1       1
4       Snake      0      0      0      0      0      0      0      0      0      7       7
5       Tiger      8      8      8      8      1      1      1      1      0      8       8
6       Mouse      1      1      1      1      1      1      1      1      0      1       1

我已经查看了r中的Fill函数,但我无法得到我想要的结果. 对如何做到这一点有什么建议吗?

推荐答案

使用base R-从‘Week’列名中提取数字后缀部分,然后获得min/max个值之间的序列(‘i2’),基于match对索引进行复制并将列名重命名为i2

i1 <- as.integer(sub("week_", "", names(test_data)[-1]))
i2 <- Reduce(`:`, as.list(range(i1)))
test_data <- cbind(test_data[1], test_data[-1][cumsum(!is.na(match(i2, i1)))])
names(test_data)[-1] <- paste0("week_", i2)

-输出

> test_data
  Participant week_0 week_1 week_2 week_3 week_4 week_5 week_6 week_7 week_8 week_9 week_10
1        Lion      5      5      5      5      1      1      1      1      1      2       2
2         Cat      0      0      0      0      0      0      0      0      0      4       4
3         Dog      1      1      1      1      1      1      1      1      6      1       1
4       Snake      0      0      0      0      0      0      0      0      0      7       7
5       Tiger      8      8      8      8      1      1      1      1      0      8       8
6       Mouse      1      1      1      1      1      1      1      1      0      1       1

对于tidyverse,一种 Select 是使用pivot_longer将其reshape 为‘Long’,使用complete来扩展数据,使用fill使用先前的非NA来reshape 缺失的值,并使用pivot_wider将其reshape 回‘Wide’

library(dplyr)
library(tidyr)
test_data %>%
  pivot_longer(cols = starts_with('week_'), 
    names_prefix = "week_", names_transform = as.integer) %>% 
  complete(Participant, name = full_seq(name, period = 1)) %>% 
  fill(value, .direction = "downup") %>%
  pivot_wider(names_from = name, values_from = value, 
   names_prefix = "week_") %>% 
  arrange(match(Participant, test_data$Participant))

-输出

# A tibble: 6 × 12
  Participant week_0 week_1 week_2 week_3 week_4 week_5 week_6 week_7 week_8 week_9 week_10
  <chr>        <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>
1 Lion             5      5      5      5      1      1      1      1      1      2       2
2 Cat              0      0      0      0      0      0      0      0      0      4       4
3 Dog              1      1      1      1      1      1      1      1      6      1       1
4 Snake            0      0      0      0      0      0      0      0      0      7       7
5 Tiger            8      8      8      8      1      1      1      1      0      8       8
6 Mouse            1      1      1      1      1      1      1      1      0      1       1

R相关问答推荐

是否可以创建一个ggplot与整洁判断的交互作用

计算满足R中条件的连续列

try 将 colored颜色 编码添加到ggploly的标题中

是否有新方法来更改Facet_WRAP(Ggplot2)中条文本的文本 colored颜色 ?

将数据集旋转到长格式,用于遵循特定名称模式的所有变量对

我是否可以使用多个变异项来构建顺序列(标记多个问题)

计算使一组输入值最小化的a、b和c的值

按组使用dummy r获取高于标准的行的平均值

如何在R中的两列以上使用联合(&U)?

从两个数据帧中,有没有办法计算R中一列的唯一值?

在直方图中显示两个变量

将`magick`对象转换为原始向量

Ggplot2水平线和垂直线的图例图标不匹配

地址部件的标准化

对多个不整齐的列使用PIVOT_LONG

将列转换为R中宽格式的单独列

如何更改曲线图图例中点和线的堆叠顺序

Download.file保留原始名称或图纸名称

符号作为模型摘要中的系数名称

R图不同 colored颜色 的回归线和散点图