[R] Shifting cells and removing blanks

arun smartpink111 at yahoo.com
Tue Mar 26 18:40:28 CET 2013


If the OP wanted a list output and if the data is what it looks like, may be this helps.

dat1<-read.table(text="
 ID  Prod1  Prod2  Prod3  Prod4  Prod5
01    A             -           B        -           C
02      -           F           -           G        -
03      H         -             -           -         J
",sep="",header=TRUE,stringsAsFactors=FALSE)
 lapply(split(dat1[,-1],dat1$ID),function(x) x[x!="-"]) 

$`1`
[1] "A" "B" "C"

$`2`
[1] "F" "G"

$`3`
[1] "H" "J"
A.K.




----- Original Message -----
From: "Nordlund, Dan (DSHS/RDA)" <NordlDJ at dshs.wa.gov>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Tuesday, March 26, 2013 1:34 PM
Subject: Re: [R] Shifting cells and removing blanks

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of ankur verma
> Sent: Tuesday, March 26, 2013 10:22 AM
> To: r-help at r-project.org
> Subject: [R] Shifting cells and removing blanks
> 
> Hi ,
> 
> I've been struggling with this problem. Initially I thought something
> like
> a na.locf would help but I'm at a dead end. I have a data set like
> this:
> 
>  ID  Prod1  Prod2  Prod3  Prod4  Prod5
> 01    A             -           B        -           C
> 02      -           F           -           G        -
> 03      H         -             -           -         J


What does "like this" mean?  Is this a text file, or a list, or a dataframe, or a matrix, or ...?  You say you want to remove all blanks.  Are the hyphens your attempt to represent blanks in the email, or do they actually exist?


> 
> 
> And I would like to remove all the blanks and get a aggregated list
> against
> each ID like so:
> 
> 01 A B C
> 02 F G
> 03 H J

Again, do you want this output in a text file or some R structure?  If an R structure, which one?

> 
> Can anyone suggest a function or a package on how to achieve this ?
> 

Not until you provide a reproducible example as the posting guide asks.


Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204

______________________________________________
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