Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dollar sign before a variable

I have this sample code to create a new data frame 'new_data' from the existing data frame 'my_data'.

new_data = NULL n = 10 #this number correspond to the number of rows in my_data conditions = c("Bas_A", "Bas_T", "Oper_A", "Oper_T") # the vector characters correspond to the target column names in my_data for (cond in conditions){     for (i in 1:n){         new_data <- rbind(new_data, c(cond, my_data$cond[i]))     } } 

The problem is that my_data$cond (where cond is a variable, and not the column name) is not accepted.

How can I call a column of a data frame by using, after the dollar sign, a variable value?

like image 587
this.is.not.a.nick Avatar asked Sep 12 '12 13:09

this.is.not.a.nick


People also ask

Can a variable start with a dollar sign?

The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an IdentifierName. As such, the $ sign may now be used freely in variable names.

What is sign before a variable?

The plus(+) sign before the variables defines that the variable you are going to use is a number variable.

Can dollar sign be used in variable name?

Variable names can be up to 100 characters long, not including the dollar sign ($). Examples of variable names are $docname and $_currentline. Although a variable must start with a letter or underscore (_), the remainder of the name can consist of letters, digits, and underscores.

What does '$' mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.


1 Answers

To access a column, use:

my_data[ , cond] 

or

my_data[[cond]] 

The ith row can be accessed with:

my_data[i, ] 

Combine both to obtain the desired value:

my_data[i, cond] 

or

my_data[[cond]][i] 
like image 82
Sven Hohenstein Avatar answered Oct 22 '22 15:10

Sven Hohenstein