Remove selected columns in R dataframe [duplicate] - r

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)

Related

How do I name rows [duplicate]

This question already has answers here:
Rename a sequence of variable names in data frame
(2 answers)
How to rename the columns names using loops in R [duplicate]
(1 answer)
Closed 2 years ago.
I have a data set hdata(as picture1 shows):
row names were automatically given by R
How do I name rows by y1 to y6 instead of v1 to v6 given by R?
We can change the column names with paste
colnames(hdata) <- paste0('y', seq_along(hdata))
Or use sub
colnames(hdata) <- sub('v', 'y', colnames(hdata))

How can I order the rownames of a dataframe in R? [duplicate]

This question already has answers here:
How can I use the row.names attribute to order the rows of my dataframe in R?
(8 answers)
Closed 3 years ago.
I am using biological data from an imported CSV file where the gene symbols are put into "column 0" as rownames. I would like to order the rows alphabetically based on gene symbol.
I was thinking of extracting the column 0 rownames to a new column and then ordering but I prefer leaving the dataset how it is. Is there anyway to order the row names instead?
row_names_test <- data.frame(cbind(genes <- rownames(read.csv("~/row_names_test.csv")),
read.csv("~/row_names_test.csv")))
row_names_test <- row_names_test[order(row_names_test$genes), ]

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

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")

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

How to delete a column in R dataframe [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Drop Columns R Data frame
Suppose, I have the following dataframe, and want to delete column "dataB" what would be R command for that?
y <- data.frame(k1=c(101,102,103,104,105,106,107,108),
B=c(11,12,13,NA,NA,16,17,18),
dataB=11:18)
This: y$B <- NULL removes column B from dataframe y.

Resources