2

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

2
  • 1
    I think you just need to remove the quotation marks around rowname, when rowname is a variable... Commented Mar 15, 2013 at 23:50
  • i made a few edits to clarify what i meant. Commented Mar 16, 2013 at 0:08

3 Answers 3

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"
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

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, just try paste("the value is ", df[rowname,1], ".") like @adibender suggested.
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.
Good you figured it out. Sorry I misunderstood your initial question.
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.