a <- "name"
df$a
Here, df is my data frame, and name is one of the column names of data frame df. How could I command R to execute code by considering (a) to be an object name instead of a character?
Do the following. First remove a column in which you want to work. After that, turn it into your desired object.
Example:
A = factor (a)
A = vector (a)
1 - You can only concatenate vectors.
2- A letter "a" is not sensitive using the name of an object. Use another name, for example: Work1
Related
in R there were several times on which a numerical vector had names before each numeric value as this:
class(oral_NO_AR_comp$clustering$clust1)
output: 1 "numeric"
and the content looks like this:
THe point here is that I need to change the names of the strings above the numbers, is there a way to do that?r
You can get those names with
names(oral_NO_AR_comp$clustering$clust1).
You can use
names(oral_NO_AR_comp$clustering$clust1)<- <whatever you want>
# or
setNames(oral_NO_AR_comp$clustering$clust1, <whatever you want)
to change the names if you like. You can also use remove the names with
unname(oral_NO_AR_comp$clustering$clust1)
Note that these functions (with the exception of names<-) do not change the original value, they return a new value. If you want to replace the original value, be sure to assign it <- to the original variable.
In R, I am trying to get input from a user to create the name of a new data frame. e.g
number <- readline(prompt = "what is your number:")
Which creates a character string with one entry, e.g number: "4"
Now i want to create a dataframe named after the character inputted, and subset some other information based on that number from another table, for example:
number_4 <- subset(df, df$NO=="4")
As i might be doing hundreds of these i do not want to have to manually name each dataframe, is there a way to use the character to name a dataframe?
We can use assign function
assign(paste0("number_", number), subset(df, NO == number))
I would like to assign names to rows in R but so far I have only found ways to assign names to columns. My data is in two columns where the first column (geo) is assigned with the name of the specific location I'm investigating and the second column (skada) is the observed value at that specific location. To clarify, I want to be able to assign names for every location instead of just having them all in one .txt file so that the data is easier to work with. Anyone with more experience than me that knows how to handle this in R?
First you need to import the data to your global environment. Try the function read.table()
To name rows, try
(assuming your data.frame is named df):
rownames(df) <- df[, "geo"]
df <- df[, -1]
Well, your question is not that clear...
I assume you are trying to create a data.frame with named rows. If you look at the data.frame help you can see the parameter row.names description
NULL or a single integer or character string specifying a column to be used as row names, or a character or integer vector giving the row names for the data frame.
which means you can manually specify the row names when you create the data.frame or the column containing the names. The former can be achived as follows
d = data.frame(x=rnorm(10), # 10 random data normally distributed
y=rnorm(10), # 10 random data normally distributed
row.names=letters[1:10] # take the first 10 letters and use them as row header
)
while the latter is
d = data.frame(x=rnorm(10), # 10 random data normally distributed
y=rnorm(10), # 10 random data normally distributed
r=letters[1:10], # take the first 10 letters
row.names=3 # the column with the row headers is the 3rd
)
If you are reading the data from a file I will assume you are using the command read.table. Many of its parameters are the same of data.frame, in particular you will find that the row.headers parameter works the same way:
a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.
Finally, if you have already read the data.frame and you want to change the row names, Pierre's answer is your solution
I recently discovered that R will output data for a column name if the column name does not exist as is passed but the dataframe has a column name that meets what was passed as column name to retrieve data.
So if you have a dataframe X with column names say fruits and vegetables and if you try to retrieve data as X$fruit it will give you the fruits column data even when the passed column name (fruit) does not match the data frame column name (fruits). It throws error if there are column names like fruitss because at this time I believe R cannot decide whether to show fruits or fruitss to the passed value of x$fruit
How to avoid this?
The $ can create confusion where there are similar prefix for column names, so it is better to use [[ or [ to extract the columns as it will match the entire string and not any partial strings.
X[["fruit"]]
Or
X[, "fruit"]
colnames gives me the column names for a whole dataframe. Is there any way to get the name of one specified column. i would need this for naming labels when plotting data in ggplot.
So say my data is like this:
df1 <- data.frame(a=sample(1:50,10), b=sample(1:50,10), c=sample(1:50,10))
I would need something like paste(colnames(df1[,1])) which obviously won't work.
any ideas?
you call the name like this:
colnames(df1)[1]
# i.e. call the first element of colnames not colnames of the first vector
however by removing your comma e.g.:
colnames(df1[1])
you can also call the names, becauseusing only [x] not [,x] or [[x]] keeps the data.frame structure not reducing to a vector unlike $x and [,x]
names(df1)[1]
will give you the name of the first column. So too will
names(df1[1])
Neither uses a comma.
Would colnames(df1)[1] solve the problem?