How do I rename the column 0 in a dataframe? [duplicate] - r

This question already has answers here:
How do I name the "row names" column in r
(2 answers)
Understanding array indexing in R
(2 answers)
Closed 3 years ago.
I have a dataframe with 4 columns. How do I give the first column a name?
I tried using dplyr but it doesn't work.
colnames(df)[0] <- "Test"

That's not a column, those are the row names. Within the tidyverse there's a function to create a column out of them:
df<-tibble::rownames_to_column(df, "test")

Related

How to drop multiple columns with similar names in a data frame [duplicate]

This question already has answers here:
How to drop columns by name pattern in R?
(6 answers)
Remove columns that contain a specific word
(5 answers)
Closed 3 years ago.
My data set has about 100 columns and some of them (about 25) are called similar but not them same. I'd like to delete all columns that start with "reads_per_million" because write this is so impractical:
data_tumor <- data_tumor[,-c(3,5,7,13,15,22,33,54,65,34,**etc,etc**)]
Thank you so much

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)

Subsetting an R Matrix [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
in R programming, how do I subset a matrix so that I can skip columns in between? I only know how to do it continuously such as 1:4, but what if I want the first, second, and fourth colum
You can select specific columns as follows:
new_df <- x[,c(1,2,4)]# Select column 1,2 and 4

Bring each value in the list to its own column [duplicate]

This question already has answers here:
How do I make a matrix from a list of vectors in R?
(6 answers)
Combine a list of data frames into one data frame by row
(10 answers)
Closed 5 years ago.
I have a dat frame called crimeLARaw_tbl_final.
One of the columns called location_1.coordinates, is a ‘list’ data type column which contains a list of values:
location_1.coordinates
c(-118.3157, 34.0454)
c(-118.2717, 34.001)
c(-118.2671, 34.0294)
c(-118.4517, 33.967)
How could I bring each value in the list to its own column?
long lat
-118.3157 34.0454
-118.2717 34.001
-118.2671 34.0294
-118.4517 33.967
I have tried with dplyr and stringr but not getting anywhere.
Thank you

Remove selected columns in R dataframe [duplicate]

This question already has answers here:
How do you remove columns from a data.frame?
(12 answers)
Remove an entire column from a data.frame in R
(8 answers)
Closed 6 years ago.
dataframename <- data.frame(
  col1=1:10,
  col2=10:1,
  col3=1:50,
  col4=11:20
)
Consider the above dataframe and I want to remove column 1 and column 4.
1. Without using any package.
2. The answer should be in dataframe format only and not vector results.
###Use subset command:###
dataframename <- subset(dataframename, select = -c(col1,col4) )
###One more approach is you can use list(NULL) to the dataframe:###
dataframename[,c("col1","col4")] <- list(NULL)

Resources