Questions tagged [r-colnames]

114 questions
1
vote
1 answer

How to use current column name in lapply function in R?

I have two data sets: The first data set contains participants' numerical answers to questions: data <- data.frame(Q1 = 1:5, Q2 = rev(1:5), Q3 = c(4, 5, 1, 2, 3)) The second data set serves as a reference table…
1
vote
1 answer

R dataframe has date as part of a column name and I want specify the most recent two columns without typing them each time

I am trying to automate a report that I am running manually each week. I have a data frame called p_wide where each week a new column is being added and column names contain dates that represent when each of those columns are added, for example: id …
tokay
  • 13
  • 2
1
vote
1 answer

Is there a way to specify flextable header labels by just listing the new header names in order?

I'm using flextable with a dataset that has many columns, and I want to use set_header_labels() to change them all at once, in order, without having to specify which name goes with each specific value. However, my code keeps running but not working.…
J.Sabree
  • 2,280
  • 19
  • 48
1
vote
2 answers

R : Change name of variable using for loop

I have a data, and vectors conatin name of variables, from these vectorsi calculate the sum of variables contained in the vector and i want to put the result in a new variables that have diffrent names let say i have three vectors >data Name …
Reda
  • 449
  • 1
  • 4
  • 17
1
vote
1 answer

Change column names of data frames stored in a list by condition

I have a list with several data frames of different taxa. The df's for the different taxa are of different length and have different names for columns with the "same information", e.g. "abundance", "abund", "individuals". I show you an…
1
vote
3 answers

How do I make the name of a list the name of its column?

I have a set of 270 RNA-seq samples, and I have already subsetted out their expected counts using the following code: for (i in 1:length(sample_ID_list)) { assign(sample_ID_list[i], subset(get(sample_file_list[i]), select = expected_count)) …
avery
  • 43
  • 4
1
vote
1 answer

Replacing same part of multiple col names in R

After creating some dummy variables, R creates some unhelpful colnames: they start with ".data_" a <- as.factor(c("green", "yellow", "blue")) b <- as.factor(c("blue", "yellow", "green")) df <- data.frame(a, b) library(fastDummies) dummy1 <-…
Elena
  • 45
  • 5
1
vote
2 answers

Subset columns in df using for loop in R

I would like to subset a dataset into several smaller df's while using the names of columns in a for-loop. Create some data (this can probably be done easier...) Line1 <- c(1:7) Line2 <- c(3:9) df <- rbind(Line1, Line2) ColumnNames <-…
1
vote
2 answers

What is the best way to retrieve a column of a data.frame with its column name?

If I use df$columnName, you only get a vector which does not have the name of the column anymore. This means names(df$columnName) --> null I could use df["columnName"], but this is kind of unhandy.. because I have to pass the columnName as string…
Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
1
vote
2 answers

How add a column to a data.frame with name extracted from an existing column in R?

I have the DF data.frame. I would like to add another column (i.e., call it station_no) where it will extract the number after underscore from the Variables column. library(lubridate) library(tidyverse) set.seed(123) DF <- data.frame(Date =…
Hydro
  • 1,057
  • 12
  • 25
1
vote
1 answer

Calculate correlation between two columns based on column names

I am trying to calculate a correlation between two columns in a dataframe, based on a matching pattern in column name. In this case, I want to get the correlation between columns 'Obs1_grp1' and 'Obs1_grp2', 'Obs2_grp1' and 'Obs2_grp2', and so on. >…
1
vote
2 answers

Manually assigning column names using read_excel in R

First off, I'm new to R, so this question won't be using if statements or anything specific. I loaded an xls document into R, and now have to open a specific sheet and call specific columns from one particular sheet. Let's call the sheet "fruit" and…
hed
  • 15
  • 7
1
vote
2 answers

R: To identify whether column names in a dataframe contains string

Say I have 2 dataframes: df1: Name Data123 Data321 Age A 123 321 20 df2: Name Age B 20 I wish to check which dataframe has column names containing the string "Data". If yes, the dataframe will be passed into a…
Agnes Lee
  • 322
  • 1
  • 12
1
vote
1 answer

Create formula inside loop over column names

I would like to loop through columns in a data set and use the name of the column to aggregate the data set. However, I am getting an error when I try to feed through the column name into the aggregate function: "Error in…
user9084595
  • 85
  • 1
  • 1
  • 5
1
vote
1 answer

Flexible Column names in knitr::kable table

Problem: Have a large df with many variables. Trying to create a summary kable table with different column names than the original variables. library(knitr) library(dplyr) knitr::kable(iris %>% head(), col.names = c("Sepal length",…
vb66
  • 353
  • 3
  • 14