I have a dataframe called test1 with some data, for example:
name address
1 xxx1 x
2 xxx2 y
3 xxx3 z
I have some code
new.function = function(dataframe, colname){
print(dataframe$colname[1])
}
#call function
new.function(test1, "name1")
I want it print out xxx1 because the function does the same as test1$name[1] but it failed. I just thinking about how to assign a column name to a variable so that it can be used in function more flexiable.
If you only want to assign the column name to a variable you can do :
variable <-names(test1)[1]
To print xx1 :
print((test1$name)[1])
You should take a look at dplyr to handle table easily
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.