1

I have a data frame where the row names are words and I can call the first column of that row of data drame using something like

>df['rowB',1]

i know I can use paste to combine a variable and a string using paste to do something like

>paste("the value is ", df['rowB',1], "."]

and that will get me an output of the string with the value of the variable. what if rowname is a variable that equals 'rowB? I tried to do a first paste to put in the paste above, but the result of the first paste doesn't evaulate to the value, but rather is just a string that says

>rowname<-'rowB'
>type<-paste("relatype[\'", rowname, "\',1]", sep="")
'df['rowB',1]'

long story short, I want to input a value called 'rowname' as a parameter of a function and have it be evaluated for the value of rowname, so I can then put that value into a string within that same function.

I'm also open to a wholly different solution. any and all suggestions are welcome.

thanks

sexp1stol
  • 445
  • 4
  • 17
user1714887
  • 147
  • 1
  • 10

3 Answers3

1

This should work:

paste("the value is ", get(df['rowname',1]), "."]

If you are not familiar, 'get' in r is similar to 'eval' in python.

x=c('a', 'c', 'b')
a=2
x[1]
'a'
get(x[1])
2
Lucas Fortini
  • 2,420
  • 15
  • 26
  • this seems close, but isn't exactly right because just using paste without the get function will get the value of the variable df['rowB',1]. i clarified my initial question to make what i want a little more clear. – user1714887 Mar 16 '13 at 00:07
  • @user1714887, just try `paste("the value is ", df[rowname,1], ".")` like @adibender suggested. – flodel Mar 16 '13 at 00:11
  • ho-ly cow! that was correct. how do i mark @adibender as the correct answer? i guess i was overcomplicating it by a huge amount. – user1714887 Mar 16 '13 at 00:13
  • Good you figured it out. Sorry I misunderstood your initial question. – Lucas Fortini Mar 16 '13 at 00:37
1

I'm afraid I don't understand the question; how is your function different from the following?

foo = function(rowname = "Species", d = t(iris)){

  paste("I'm selecting", d[rowname, 1])

}

foo()
# [1] "I'm selecting setosa"
baptiste
  • 75,767
  • 19
  • 198
  • 294
1

Not sure what the problem might be, not entirely clear from your description, but if rowname is a variable, you don't need anything special, because it will evaluate to it's value anyway. Let

mat <- matrix(1:10, nrow = 5)
rownames(mat) <- letters[1:5]
mat
##   [,1] [,2]
##a    1    6
##b    2    7
##c    3    8
##d    4    9
##e    5   10

and rowname <- "b", then

rowname
##[1] "b"

so

mat[rowname, 1]
##b 
##2 

which is the same as mat["b", 1]. It only fails, if you use mat['rowname', 1]. If you want to put this in functions, you can do something like:

getElement <- function(mat, row.name, column.index) {

    mat[row.name, column.index]

}


getElement(mat, "b", 1)
##b 
##2 

pasteSenstence <- function(mat, row.name, col.index) {

    paste("The element of row", row.name, "and column", col.index, "is", 
            getElement(mat, row.name, col.index))

}
pasteSentence(mat, "b", 1)
##[1] "The element of row b and column 1 is 2"

which also works with rowname <- "b"

 pasteSentence(mat, rowname, 1)
   ##[1] "The element of row b and column 1 is 2"
adibender
  • 7,288
  • 3
  • 37
  • 41