Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何更改名称在值范围内的有序列组?_R_Mutate - Fatal编程技术网

R 如何更改名称在值范围内的有序列组?

R 如何更改名称在值范围内的有序列组?,r,mutate,R,Mutate,我正在使用下面的数据框架,其中包含1997-2010年每年的列变量数据,以及由列“min”和“max”描述的年份范围 如果年份在最小值到最大值的范围内,我希望将我在每个年份列中的值更改为1。我将如何执行此操作 library(tidyverse) df <- structure(list(`1997` = c(1, 0, 0, 0, 0, 0), `1998` = c(0, 0, 0, 0, 0, 0), `1999` = c(0, 0, 0, 0, 0, 0), `2000`

我正在使用下面的数据框架,其中包含1997-2010年每年的列变量数据,以及由列“min”和“max”描述的年份范围

如果年份在最小值到最大值的范围内,我希望将我在每个年份列中的值更改为1。我将如何执行此操作

library(tidyverse)

    df <- structure(list(`1997` = c(1, 0, 0, 0, 0, 0), `1998` = c(0, 0, 
0, 0, 0, 0), `1999` = c(0, 0, 0, 0, 0, 0), `2000` = c(0, 0, 0, 
1, 0, 1), `2001` = c(0, 0, 0, 1, 0, 1), `2002` = c(0, 0, 0, 0, 
0, 1), `2003` = c(0, 0, 0, 0, 0, 1), `2004` = c(0, 0, 0, 0, 0, 
1), `2005` = c(0, 0, 0, 1, 0, 1), `2006` = c(0, 0, 1, 0, 0, 1
), `2007` = c(0, 0, 1, 1, 0, 1), `2008` = c(0, 0, 1, 1, 0, 1), 
    `2009` = c(0, 0, 1, 1, 0, 1), `2010` = c(0, 0, 1, 1, 0, 1
    ), min = c(1997, 1998, 2006, 2000, 1997, 2000), max = c(1998, 
    1998, 2010, 2010, 2008, 2010)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("1997", "1998", "1999", "2000", 
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", 
"2009", "2010", "min", "max"))

一种解决方案是使用
sapply
mapply
,如下所示。我还使用了
dplyr
中的
between
函数

而不是
0
1
我的解决方案显示
FALSE/TRUE
。希望这对OP是好的

#df has been taken from OP
sapply(names(df)[1:(ncol(df)-2)], 
      function(x)mapply(between, as.numeric(x), df$min, df$max)) %>%
       as.data.frame() %>% cbind(df[,c("min","max")])

   1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  min  max
1  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 1997 1998
2 FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 1998 1998
3 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE 2006 2010
4 FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE 2000 2010
5  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE 1997 2008
6 FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE 2000 2010

有趣的问题。我希望您不会介意最终数据。框架显示的是
TRUE/FALSE
,而不是
1/0
#df has been taken from OP
sapply(names(df)[1:(ncol(df)-2)], 
      function(x)mapply(between, as.numeric(x), df$min, df$max)) %>%
       as.data.frame() %>% cbind(df[,c("min","max")])

   1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  min  max
1  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 1997 1998
2 FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 1998 1998
3 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE 2006 2010
4 FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE 2000 2010
5  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE 1997 2008
6 FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE 2000 2010