[R] do.call

arun smartpink111 at yahoo.com
Thu Dec 6 16:00:54 CET 2012



Hi Dominic,
In that case, this might help you.
set.seed(15)
datos<-as.data.frame(matrix(sample(c(1:20,NA),30,replace=TRUE),ncol=6))
do.call(rbind,lapply(split(datos,1:nrow(datos)),function(x) paste(x[!is.na(x)],collapse=";")))
 # [,1]              
#1 "13;3;18;17;14"   
#2 "5;18;14;10;17;3" 
#3 "6;11;8;4"        
#4 "14;15;15;3;2;20" 
#5 "8;18;19;17;12;11"
A.K.


----- Original Message -----
From: Dominic Roye <dominic.roye at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Thursday, December 6, 2012 9:33 AM
Subject: Re: [R] do.call

Well, my aim is to get a new column that looks like this without NA
but with all of the values in the columns and rows:

200.4;608.1; ...
360.0
420.6;608.0; ...
100.1;905.0
.
.
.

in the original data, each value has its own column. Each column has
different number of values, therefore arise NA.

Thanks for your help!


2012/12/6 arun <smartpink111 at yahoo.com>:
> HI Dominic,
>
> You are right. na.omit() will delete rows containing at least 1 NAs.  Suppose, a function deletes  only NAs separately for each column, then the length of each column will be different.
> According to ?data.frame()
> " Objects passed to ‘data.frame’ should have the same number of
>      rows, but atomic vectors, factors and character vectors protected
>      by ‘I’ will be recycled a whole number of times if necessary
>      (including as elements of list arguments).
> "
> Then, instead of data.frame, list would be an option.
>
>
> Could you tell me the expected output?  I thought you wanted to eliminate rows which have at least 1 NA.
> If your dataset includes NA in all the rows, then it would be better to have some threshold. For e.g.
> set.seed(15)
>  datos<-as.data.frame(matrix(sample(c(1:20,NA),30,replace=TRUE),ncol=6))
> do.call(paste,c(datos[apply(datos,1,function(x) !sum(is.na(x))>1),],sep=";")) #deleted rows with more than 1 NA
> #[1] "13;NA;3;18;17;14" "5;18;14;10;17;3"  "14;15;15;3;2;20"  "8;18;19;17;12;11"
>  A.K.
>
>
>
>
>
>
> ----- Original Message -----
> From: Dominic Roye <dominic.roye at gmail.com>
> To: arun <smartpink111 at yahoo.com>
> Cc: R help <r-help at r-project.org>
> Sent: Thursday, December 6, 2012 7:40 AM
> Subject: Re: [R] do.call
>
> Hi,
>
> thanks for your answer, but in this way R delete all rows with NA. In
> all rows I have values but also NA. I don´t know why it delete all of
> them.
>
> I hope you can give me an idea.
>
> Thanks.
>
> Best regards,
>
>> str(temp)
> 'data.frame':   112598 obs. of  35 variables:
> $ Lista.de.códigos.de.diagnóstico: chr  "218.0" "890.1" "998.89" "650." ...
> $ X                              : chr  NA "E986" "780.2" "V27.0" ...
> $ X.1                            : chr  NA NA "780.4" NA ...
> $ X.2                            : chr  NA NA "381.3" NA ...
> $ X.3                            : chr  NA NA NA NA ...
> $ X.4                            : chr  NA NA NA NA ...
> $ X.5                            : chr  NA NA NA NA ...
> $ X.6                            : chr  NA NA NA NA ...
> $ X.7                            : chr  NA NA NA NA ...
> $ X.8                            : chr  NA NA NA NA ...
> $ X.9                            : chr  NA NA NA NA ...
> $ X.10                           : chr  NA NA NA NA ...
> $ X.11                           : chr  NA NA NA NA ...
> $ X.12                           : chr  NA NA NA NA ...
> $ X.13                           : chr  NA NA NA NA ...
> $ X.14                           : chr  NA NA NA NA ...
> $ X.15                           : chr  NA NA NA NA ...
> $ X.16                           : chr  NA NA NA NA ...
> $ X.17                           : chr  NA NA NA NA ...
> $ X.18                           : chr  NA NA NA NA ...
> $ X.19                           : chr  NA NA NA NA ...
> $ X.20                           : chr  NA NA NA NA ...
> $ X.21                           : chr  NA NA NA NA ...
> $ X.22                           : chr  NA NA NA NA ...
> $ X.23                           : num  NA NA NA NA NA NA NA NA NA NA ...
> $ X.24                           : chr  NA NA NA NA ...
> $ X.25                           : chr  NA NA NA NA ...
> $ X.26                           : num  NA NA NA NA NA NA NA NA NA NA ...
> $ X.27                           : chr  NA NA NA NA ...
> $ X.28                           : chr  NA NA NA NA ...
> $ X.29                           : num  NA NA NA NA NA NA NA NA NA NA ...
> $ X.30                           : num  NA NA NA NA NA NA NA NA NA NA ...
> $ X.31                           : num  NA NA NA NA NA NA NA NA NA NA ...
> $ X.32                           : num  NA NA NA NA NA NA NA NA NA NA ...
> $ X.33                           : logi  NA NA NA NA NA NA ...
>
>> new <- do.call(paste, c(na.omit(temp[,1:35]), sep = ";"))
>> new
> character(0)
>
>
> 2012/12/4 arun <smartpink111 at yahoo.com>:
>> Hi,
>> Try this:
>> set.seed(15)
>> datos<-as.data.frame(matrix(sample(c(1:20,NA),30,replace=TRUE),ncol=6))
>>  do.call(paste,c(na.omit(datos),sep=";"))
>> #[1] "5;18;14;10;17;3"  "14;15;15;3;2;20"  "8;18;19;17;12;11"
>>
>> A.K.
>>
>>
>>
>> ----- Original Message -----
>> From: Dominic Roye <dominic.roye at gmail.com>
>> To: r-help at r-project.org
>> Cc:
>> Sent: Tuesday, December 4, 2012 7:38 AM
>> Subject: [R] do.call
>>
>> Hello,
>>
>> I have a problem with the "do.call-function". I would like to merge
>> the values of more than 30 columns, but not in all of the rows exist
>> values, so with this commando i get a lot of ";" or NA.
>>
>> How get i only the merge of cells with a number?
>>
>>
>> datos$NEW <- do.call(paste, c(datos[,19:53], sep = ";"))
>>
>>
>> $ NEW                              : chr
>> "218.0;;;;;;;;;;;;;;;;;;;;;;;;NA;;;NA;;;NA;NA;NA;NA;NA
>>
>>
>> I hope you can help me. Thanks!
>>
>> Best regards,
>>
>> Dominic
>>
>> ______________________________________________
>> R-help at r-project.org 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