Solved

R program matrix

Posted on 2015-01-11
2
203 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
Comment Utility
> 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 71
mapShare challenge 13 67
wordlen challenge 3 46
Device same like our heart 12 43
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now