Solved

R program matrix

Posted on 2015-01-11
2
224 Views
Last Modified: 2015-01-13
Hi,

I am trying to create a table that looks as follows:

    portfolio            return            risk
    SP                        6                    20
    NASDAQ            10                  25
The numbers are stored in variables and I am not hardcoding them and rest are headings.
How do I code for this.

Thanks
0
Comment
Question by:camper12
  • 2
2 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40545273
> t <- matrix(c(60,20,10,25),ncol=2,byrow=TRUE)
> rownames(t) <- c("SP","NASDAQ")
> colnames(t) <- c("return","risk")
> t <- as.table(t)
> t

substitute your variables for the corresponding values in the collection
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40545287
another version, same idea, using a matrix, but different syntax

> vals <- c(6,20,10,25)
> rows <- c("SP","NASDAQ")
> cols <- c("return","risk")
> tab <- matrix(vals,ncol=2,byrow=TRUE,dimnames=list(rows,cols))
> tab

I don't know of a way to put a label above the row names, that is, your "portfolio" label.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question