Change Column name of a dataset in R [duplicate] - r

This question already has answers here:
Changing column names of a data frame
(18 answers)
Closed 8 years ago.
I'm a complete newbie and am trying to change column names of a dataset in R. For example, to change the column name of 'Eth' in dataset quine to 'Ethnic'. Any help or the name of the function is greatly appreciated.

The function 'colnames' is what you're looking for
let's say that 'Eth' is the third column, do
colnames(dataset)[3]<-"Ethnic"
colnames(dataset) returns exactly what you think it should, but you can also use it to set column names.
Doing the following
colnames(dataset)<-newColNames
where newColNames is a vector of names of the same length as the number of columns in dataset will change all the column names in order.
The following (as I did above)
colnames(dataset)[i]<-name
where name is a string and i is an integer, will change the name of the ith column to whatever the string "name" is

Related

Trouble converting Values in Column into Row Names of Data Frame in R [duplicate]

This question already has answers here:
Why am I getting X. in my column names when reading a data frame?
(5 answers)
data.frame without ruining column names
(2 answers)
Closed last month.
I am trying to convert the first column of a data frame as Row names.
It works fine but the names of the data frame format changes!
It changes from like 100-21-0 to X100.21.0
First column is Character values: Code, CBT, DQY, DQX etc.
and the names (or the first row?) of the data frame (double) like: Code, 100-21-0, 1002-84-2, 100-47-0 etc.
Code
100-21-0
CBT
0
DQY
1
I am using the code similar to:
newdataframe <- data.frame(dataframe, row.names = 1)
It works fine but the names of the data frame change from 100-21-0, 1002-84-2, 100-47-0 to X100.21.0, X1002.84.2, X100.47.0 !!!
I am confused why? Can anyone help on this?

operating R dataframe using variables for the column name [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 8 months ago.
I want to store a column name in a variable and operate a dataframe based on that column name.
For example if a I have two columns named car_sales and airplane_sales. I have a variable var that a user sets to say car_sales. i then calculate a new column like so:
calc_col <- paste0(var,"_delta")
df$calc_col <- abs(df$var - lag(df$var ,12))
The var will change based on user input, so the resulting column will also change
How do I do this in R?
You could use:
df[[calc_col]] <- abs(df[[var]] - lag(df[[var]], 12))

How can I change the names of multiple columns in r using paste0 [duplicate]

This question already has answers here:
Renaming multiple columns with indices in R
(3 answers)
Closed 3 years ago.
I have a dataframe which has 50 columns and I am trying to change the name of half of the columns to include the word "female_" in the title. What code can I use to change the name of multiple columns?
paste is vectorized. So, it can be directly changed with concatenating a string into it and updating the relevant column names
names(df1)[1:25] <- paste0("female_", names(df1)[1:25])
NOTE: Here, we are taking the first 25 column names (as the position is not specified)

subset whole data frame for value and return rows in which value are found [duplicate]

This question already has answers here:
Finding rows containing a value (or values) in any column
(3 answers)
Closed 6 years ago.
I am trying to subset a data frame containing 626 obs. of 149 variables and I want to look for a specific string and return the rows that have that value regardless of what column it is found in.
For example:
I am looking for this string "GO:0004674" in a data frame that can contain this string in many different columns and rows as shown below in the image link.
For example the string "GO:0004674" can be found in row 12, 13 and 14. So I would want to keep only those rows and later on export them.
How can I perform this? All examples that I have seen thus far only look for string in a specific column and not in the whole dataframe.
Ant help will be greatly appreciated.
You can use apply to do row-wise operation using the argument MARGIN = 1. Example:
mydf[apply(mydf, MARGIN = 1, FUN = function(x) {"GO:0004674" %in% x}), ]

How can I keep only rows with a certain name in R? [duplicate]

This question already has answers here:
Subsetting matrices
(3 answers)
Closed 8 years ago.
I have a large data set in the form of a matrix, each row has its own unique name. I have a list of row names where I want to keep that data in that row. Is there a way that I can keep only the rows of my matrix that have row names in common with my list of row names? i.e. Can I throw out any row that does not have a row name in my list and be left with a matrix of the rows with names from my list?
Any help would be greatly appreciated! My current method is slow and very circuitous.
if dat is your data frame and names.to.keep is a vector containing the names of the rows which you want, then
dat.keep = dat[rownames(dat) %in% names.to.keep, ]
should do what you want.

Resources