[R] ordering and plotting question

Marc Schwartz MSchwartz at medanalytics.com
Mon Feb 2 23:36:56 CET 2004


On Mon, 2004-02-02 at 14:13, Simon Melov wrote:
> Hi,
> I am trying to plot several rows out of a list of thousands. I have 40 
> columns, and about 16,000 rows with the following Df structure.
> 
> ID X01 X02 X03..X40
> AI456 45 64 23...
> AI943 14 3 45 ..
> AI278 78 12 68..
> BW768 -2 -7 34..
> ...
> 
> My question is, I have a list of 100 IDs generated elsewhere 
> (Df-"Ofinterest"), I would like to plot the 100 IDs from that data 
> frame over the 40 columns (40 points per ID, with each column being 1 x 
> value). But I cant figure out how to retrieve the values from the main 
> Df and plot them. Ive tried looking at order, rank etc, but these seem 
> to apply to numbers, and not text strings.
> 
> thanks
> 
> Simon.


I am not entirely sure what type of plot you want, however the following
will enable you to subset the main dataframe, based upon matching ID's:

# Create an example vector of the ID's you want
df.index <- c("AI456", "AI278", "BW768")

> df.index
[1] "AI456" "AI278" "BW768"
 
# Create a df with the subset of data you have above
df <- data.frame(ID = c("AI456", "AI943", "AI278", "BW768"),
                 X01 = c(45, 14, 78, -2),
                 X02 = c(64, 3, 12, -7),
                 X03 = c(23, 45, 68, 34))

> df
     ID X01 X02 X03
1 AI456  45  64  23
2 AI943  14   3  45
3 AI278  78  12  68
4 BW768  -2  -7  34

# Use which() and %in% to get a vector containing the
# row indices in df that match the three entries in df.index
found <- which(df$ID %in% df.index)

> found
[1] 1 3 4

# Now subset df to include only those rows that match
MySubset <- df[found, ]

> MySubset
     ID X01 X02 X03
1 AI456  45  64  23
3 AI278  78  12  68
4 BW768  -2  -7  34


MySubset now contains only those rows that match based upon your IDs in
the "index" vector.

If you know how to generate the plot you want from here, have at it. If
not, let me know what you wish to do and I can help further.

See ?which and ?%in% for more information. Also, see ?subset for
additional ways to subset dataframes using more complex logicals.

HTH,

Marc Schwartz




More information about the R-help mailing list