This question already has answers here:
Rename multiple columns by names
(20 answers)
Closed 4 years ago.
Data frame with 4 columns and want to replace 2nd and 3rd column names only.
data frame=df
col.names =A,B,C,D
New col.names= Z,F
i have tried with the below code :
colnames(df)[2]<-"Z"
colnames(df)[3]<-"F"
but is there any possibility to rename with single line of code ?
Actual data frame contains 150+ colnames, so searching for better solution.
As it is a data.frame, names can also work in place of colnames as names of a data.frame is the column names. Subset the column names with index [2:3] (if it is a range of columns or use [c(2, 3)]) and assign it to the new column names by concatenating (c) names as a vector
names(df)[2:3] <- c("Z", "F")
Related
This question already has answers here:
Rename Columns with names from another data frame
(3 answers)
How can I rename all columns of a data frame based on another data frame in R?
(3 answers)
Closed 2 years ago.
I have a dataset called "df" and it has 5 variables called year, v1,v2,v3,v4. I have also another dataset (i.e. df_name) including two columns of old_name and new_names. The old_names contains the current name of the variables in "df" and the new_names contains the variable names that I want to assign to variable names of the "df".
So, I am trying to find a solution by which, the code looks for the variable names in "df" that are in the old_names variable of df_name and replace it with the corresponding new_names value. In fact, I am expecting something like "df_expected"
In my real dataset, I have more than 1000 variables, so I have to use the old_name and new_names in df_names that is, I cannot refer to each variable name individually.
Thanks in advance for your help.
I tried to use the solution here: Rename Columns with names from another data frame, however, it did not work. Applying the code to my case like so
names(df)[match(df_names[,"old_names"], names(df))] = df_names[,"new_names"]
returns the error:
#> Error in names(df)[match(df_names[, "old_names"], names(df))] = df_names[, : NAs are not allowed in subscripted assignments
df <- data.frame(year = 2019:2020, v1=1:3, v2=4:6, v3=7:9, v4=10:12)
df_names <- data.frame(old_names = c("v1","v2","v3","v4","v5"),new_names = c("A","B","C","D","E"))
df_expected <- data.frame(year = 2019:2020, A=1:3, B=4:6, C=7:9, D=10:12)
you can set name in data.table without mentioning old names
library(data.table)
setDT(df)
setnames(df, c('year','A','B','C','D'))
This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 2 years ago.
I have a dataframe X with column names such as
1_abc,
2_fgy,
27_msl,
936_hhq,
3_hdv
I want to just keep the numbers as the column name (so instead of 1_abc, just 1). How do I go about removing it while keeping the rest of the data intact?
All column names have underscore as the separator between numeric and character variables. There are about 400 columns so I want to be able to code this without using specific column name
You may use sub here for a base R option:
names(df) <- sub("^(\\d+).*$", "\\1", names(df))
Another option might be:
names(df) <- sub("_.*", "", names(df))
This would just strip off everything from the first underscore until the end of the column name.
This question already has answers here:
Simplest way to get rbind to ignore column names
(2 answers)
Closed 2 years ago.
is there a way to extract column names from a df and convert into a vector? In fact, what I am trying to do is rbing two df's, the second has no names so returning a name matching error? Maybe there is an easier way to copy the df1 column names to df2 so rbind will work?
In one line:
df3 <- rbind(df1, setNames(df2, names(df1)))
This question already has answers here:
Subset data to contain only columns whose names match a condition
(10 answers)
Closed 3 years ago.
I have a dataframe dat that has many variables like
"x_tp1_y"
"g_tp1_z"
"f_tp2_h"
I would like to extract elements that include "tp1".
I already tried this:
grep("tp1", dat)
grepl("tp1", dat)
dat["tp1",]
I just want R to give me elements with this pattern so I do not have to type in all variable names that are in the dataframe dat.
Like this:
command that extracts elements with pattern "tp1"
R returns parts of the dataframe that have pattern "tp1":
x_tp1_y g_tp1_z
1 2
0 3
And then I would like to create a new dataframe.
I know that I just can use
newdat <- data.frame( dat[[1]], dat[ c(1:30)])
but I have so many elements in my dataframe that this would take ages.
Thank you for your help!
dat[,grep("tp1", colnames(dat))]
grep finds the index numbers in the column names of the data.frame (the vector colnames(dat)) that contain the necessary pattern. "[" subsets
This question already has answers here:
How to extract columns with same name but different identifiers in R
(3 answers)
Closed 3 years ago.
I have a very large dataset. Of those, a small subset have the same column name with an indexing value that is numeric (unlike the post "How to extract columns with same name but different identifiers in R" where the indexing value is a string). For example
Q_1_1, Q_1_2, Q_1_3, ...
I am looking for a way to either loop through just those columns using the indices or to subset them all at once.
I have tried to use paste() to write their column names but have had no luck. See sample code below
Define Dataframe
df = data.frame("Q_1_1" = rep(1,5),"Q_1_2" = rep(2,5),"Q_1_3" = rep(3,5))
Define the Column Name Using Paste
cn <- as.symbol(paste("Q_1_",1, sep=""))
cn
df$cn
df$Q_1_1
I want df$cn to return the same thing as df$Q_1_1, but df$cn returns NULL.
If you are just trying to subset your data frame by column name, you could use dplyr for subseting all your indexed columns at once and a regex to match all column names with a certain pattern:
library(dplyr)
df = data.frame("Q_1_1" = rep(1,5),"Q_1_2" = rep(2,5),"Q_1_3" = rep(3,5), "A_1" = rep(4,5))
newdf <- df %>%
dplyr::select(matches("Q_[0-9]_[0-9]"))
the [0-9] in the regex matches any digit between the _. Depending on what variable you're trying to match you might have to change the regular expression.
The problem with your solution was that you only saved the name of your columns but did not actually assign it back to the data frame / to a column.
I hope this helps!