为什么以下代码返回空列表而不是奇数位值的列表?
library(dplyr)
library(tidyr)
### NOTE: the 'Year' has been modified from the OP.
d1 <- structure(list(Individual = 1201:1206, Wave = c(6L, 6L, 6L, 6L,
6L, 6L), Country = c(32L, 32L, 32L, 32L, 32L, 32L), Year = c(2012,
2011, 2010, 2009, 2008, 2007), `2000` = c(45.19665424, 45.19665424,
45.19665424, 45.19665424, 45.19665424, 45.19665424), `2001` = c(176.2932337,
176.2932337, 176.2932337, 176.2932337, 176.2932337, 176.2932337
), `2002` = c(9.601447666, 9.601447666, 9.601447666, 9.601447666,
9.601447666, 9.601447666), `2003` = c(259.2992188, 259.2992188,
259.2992188, 259.2992188, 259.2992188, 259.2992188), `2004` = c(4.357976722,
4.357976722, 4.357976722, 4.357976722, 4.357976722, 4.357976722
), `2005` = c(1.955436508, 1.955436508, 1.955436508, 1.955436508,
1.955436508, 1.955436508), `2006` = c(1.865651073, 1.865651073,
1.865651073, 1.865651073, 1.865651073, 1.865651073), `2007` = c(61.65472296,
61.65472296, 61.65472296, 61.65472296, 61.65472296, 61.65472296
), `2008` = c(34.62974414, 34.62974414, 34.62974414, 34.62974414,
34.62974414, 34.62974414), `2009` = c(32.96903414, 32.96903414,
32.96903414, 32.96903414, 32.96903414, 32.96903414), `2010` = c(6.761739867,
6.761739867, 6.761739867, 6.761739867, 6.761739867, 6.761739867
), `2011` = c(0, 0, 0, 0, 0, 0), `2012` = c(12.05299366, 12.05299366,
12.05299366, 12.05299366, 12.05299366, 12.05299366)), .Names = c("Individual",
"Wave", "Country", "Year", "2000", "2001", "2002", "2003", "2004",
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012"
), row.names = 1201:1206, class = "data.frame")
d2 <-
d1 %>%
gather(col_year, col_val, -c(Individual:Year)) %>% # Change from wide to deep format
filter(col_year <= Year) %>% # Only need rows <= Year
group_by(Individual) %>%
arrange(Individual, desc(col_year)) %>% # Sort to create ordered years per Individual
mutate(rn = row_number()-1, new_col = ifelse(rn==0, 'T', paste0('T.', rn))) %>% # Number rows
filter(rn < 4) %>% # Keep top 4 rows
ungroup %>% # remove grouping
select(-rn, -col_year) %>% # Remove un-needed columns
spread(new_col, col_val) # Reformat results in wide format using new col name.
d2
Source: local data frame [6 x 8]
## Individual Wave Country Year T T.1 T.2 T.3
## <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1201 6 32 2012 12.05299 0.000000 6.761740 32.969034
## 2 1202 6 32 2011 0.00000 6.761740 32.969034 34.629744
## 3 1203 6 32 2010 6.76174 32.969034 34.629744 61.654723
## 4 1204 6 32 2009 32.96903 34.629744 61.654723 1.865651
## 5 1205 6 32 2008 34.62974 61.654723 1.865651 1.955437
## 6 1206 6 32 2007 61.65472 1.865651 1.955437 4.357977
答案 0 :(得分:2)
您正在使用不可变列表,不可变意味着该对象无法更改。
您的代码:
list_odd:+arr(arr_index)
它不会使用arr(arr_index)的值更改list_odd,而是添加一个新的List实例,并添加值。
尝试在odd_concat()中插入该代码,如下所示:
def f(arr:List[Int]) : List[Int] = {
def odd_concat(list_odd:List[Int], arr_index:Int) : List[Int] = {
if(arr_index == arr.size) {
list_odd
}
else if(arr_index % 2 == 0) {
odd_concat(list_odd, arr_index + 1)
}
else {
//println(arr(arr_index))
odd_concat(list_odd:+arr(arr_index), arr_index + 1)
}
}
odd_concat(List(), 0)
}