[R] df manipulation

Henrique Dallazuanna wwwhsd at gmail.com
Thu Jul 19 14:46:36 CEST 2007


Hi, see below:

> df
  index value
1     1     1
2     4     6
3     7     4
4     9     5
5    11     3
6    13     2

> foo <- function(x){
+     index <- ifelse(x %in% df$index, df$value[which(df$index %in% x)], NA)
+     return(index)
+ }

> df_ok <- data.frame(index=1:13, value=sapply(1:13, foo))
> df_ok
   index value
1      1     1
2      2    NA
3      3    NA
4      4     6
5      5    NA
6      6    NA
7      7     4
8      8    NA
9      9     5
10    10    NA
11    11     3
12    12    NA
13    13     2

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O


On 19/07/07, Nikola Markov <markov at lyon.inserm.fr> wrote:
> I have multicolumn data.frames with the first comumn giving ordinal
> observation index ( ex.: 1 4 7 9 11 13 etc). I would like to fill up the
> missing observations (i.e. 2 3 5 6 8 etc) with "NA"s.
> Thank you
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list