How to delete a column in R dataframe [duplicate] - r

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.

Related

Select only numeric variables of a data frame in R [duplicate]

This question already has answers here:
Why does apply convert logicals in data frames to strings of 5 characters?
(2 answers)
Selecting only numeric columns from a data frame
(12 answers)
Closed 2 years ago.
I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
try this :
names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]

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

R - Create a new data frame with removing all the "NAN" element [duplicate]

This question already has answers here:
remove or find NaN in R
(4 answers)
Closed 5 years ago.
Mymatrix is a data frame and remove several columns from this frame. Then I also want to remove all the NAN element from the new data frame. But the code below does not show a correct answer.
OKHS <- Mymatrix[,c(-6,-7,-12,-13,-14),na.rm=TRUE]
Suggested code:
OKHS <- na.omit(Mymatrix[,c(-6,-7,-12,-13,-14)])

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)

Select rows from a data frame where any variable is not NA [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Removing empty rows of a data file in R
Suppose I have a dataframe df
I would like to select the rows from it, where any of the variables in the row are not NA. That is to say I only want to exclude the rows in which all the variables are NA
df[apply(!is.na(df), 1, any), ]

Resources