Gladdys Gotherin
Gladdys Gotherin

Reputation: 167

Appending variables in R

So I have this output in R, just random numbers like this

1 2 3 4 5
0.1 0.2 0.3 0.4 0.5
0.01 0.02 0.03 0.04 0.05

I want to append "Value" "Median" "Min" to this so it would like this

Value 1 2 3 4 5
Median 0.1 0.2 0.3 0.4 0.5
Min 0.01 0.02 0.03 0.04 0.05

I'm not sure how to do this...I can do it if its numbers but cant do it with words Thanks

Upvotes: 0

Views: 1452

Answers (3)

topchef
topchef

Reputation: 19783

Keeping it as close to your definition/format as possible try this:

row1 = c(1, 2, 3, 4, 5)
row2 = c(0.1, 0.2, 0.3, 0.4, 0.5)
row3 = c(0.01, 0.02, 0.03, 0.04, 0.05)   
rows = rbind(row1,row2,row3)
rownames(rows) = c('Value','Median','Mean')
rows

       [,1] [,2] [,3] [,4] [,5]
Value  1.00 2.00 3.00 4.00 5.00
Median 0.10 0.20 0.30 0.40 0.50
Mean   0.01 0.02 0.03 0.04 0.05

Upvotes: 1

ogroendal
ogroendal

Reputation: 353

You cant add a text variable to the start of your number vector, because the number vector doesnt allow some values to be characters. You can name the three vectors, the appropriate name, and then join them in a data.frame, for easy display.

value  <-  c(1,2,3)
median <-  c(0.1,0.2,0.3)
Min    <-  c(0.01,0.02,0.03)
x      <-  data.frame(value,median,min)

Outputs:

x
  value median  Min
1     1    0.1 0.01
2     2    0.2 0.02
3     3    0.3 0.03

Upvotes: 1

nadizan
nadizan

Reputation: 1373

Either paste():

> paste("Value",1,2,3,4,5)
[1] "Value 1 2 3 4 5"

Or cat():

> cat("Value",1,2,3,4,5)
Value 1 2 3 4 5

Upvotes: 2

Related Questions