Column names in vector are missing


username

I have a dataframe with this format:

DF1 <- DF0[i,6:12]  (DF0 is the original data frame I read from)

The resulting DF1 has the following names:

DF1:  
home - train -bus -car  .. - male - female
1       3      0              0       1

What I want to do is restore the names of the columns that are 0, for example:

colNull <- colnames(DF1[,DF1==0]) 

My understanding is that this generates a series of "values" (since in most DF0 rows, several columns are 0):

colNull   like  'bus','male'.....   (char type)

However, when there is only one column in DF1 with 0, the column name is no longer restored, and the result colNull is NULL (class(colNull is also NULL)), which I cannot use.

Is there an easy fix? I would also like to understand why this is happening, or what I am doing wrong. I know this has to do with the object format, not the reasoning behind it.

Thanks in advance, p.

Akron

When we subset a dataset with a single row or if the output is a single column, reduce it to a vector when subsetting. Based on the default options ?"["are

  x[i, j, ... , drop = TRUE]

so eithernames

 names(DF1[,DF1==0])
 #[1] "bus"  "male"

or using drop=FALSEshould work

 colnames(DF1[,DF1==0, drop=FALSE])
 #[1] "bus"  "male"

Or another option is to use Convert Logical Matrix to Logical Vector and as.vectorit should work as expected.

 colnames(DF1[as.vector(DF1==0)])
 #[1] "bus"  "male"

Note: this is not the general case, since there are more than one line, we may need to usecolSums(DF1)==0

data

 DF1 <- data.frame(home=1, train=3, bus=0, male=0, female=1)

Related


Column names in vector are missing

username I have a dataframe with this format: DF1 <- DF0[i,6:12] (DF0 is the original data frame I read from) The resulting DF1 has the following names: DF1: home - train -bus -car .. - male - female 1 3 0 0 1 What I want to

Column names in vector are missing

username I have a dataframe with this format: DF1 <- DF0[i,6:12] (DF0 is the original data frame I read from) The resulting DF1 has the following names: DF1: home - train -bus -car .. - male - female 1 3 0 0 1 What I want to

Column names in dataframe are missing

Claire I am developing a Shiny application. Part of the user interface involves using dropdown menus, checkboxes, etc. to select which of the columns of data to plot. I use grepl() with user input to shrink the large dataframe to the last column to plot. Speci

Fill in missing column names - Python

Graeme Prentice-Mott I'm trying to join a bunch of dataframes together and they all have the same information. But some column names are missing and some dataframes have extra columns. However, they all follow the same order for the columns they do have. I wan

Fill in missing column names - Python

Graeme Prentice-Mott I'm trying to join a bunch of dataframes together and they all have the same information. But some column names are missing and some dataframes have extra columns. However, they all follow the same order for the columns they do have. I wan

Reduce a list with a vector of column names

Christian Skyot I have the following code . standin1<-cbind(c("AAPL","JPM"), c("MSFT","AMZN")) wantedstocks<-unique(c(standin1)) wantedstockprices<-tq_get(wantedstocks,"stock.prices", from = "2010-01-01", to = "2012-01-01") prices_tbl1<-split(wantedstockprices

Replace NA with a vector of column names

basil I have a dataframe with NAs columns that I use replace_na. The problem is that these column names may change in the future, so I want to put these column names in a vector and then use that vector in the replace_nafunction . I don't want to change the wh

Pandas: print column names, missing values

LinearLeopard: I am trying to print or get a list of column names with missing values. E.g data1 data2 data3 1 3 3 2 NaN 5 3 4 NaN I want to get ['data2', 'data3']. I wrote the following code: print('\n'.join(map( lambda x :

Blank (or missing) column names in datatable with child rows

Andy Robertson I'm creating a data table in R that has sub-rows, manually defined column names and a few columns hidden. According to the example provided in the DT manual https://rstudio.github.io/DT/002-rowdetails.html , this seems fairly straightforward (ie

Pandas: print column names, missing values

Linear Panther I am trying to print or get a list of column names with missing values. E.g data1 data2 data3 1 3 3 2 NaN 5 3 4 NaN I want to get ['data2', 'data3']. I wrote the following code: print('\n'.join(map( lambda x :

Blank (or missing) column names in datatable with child rows

Andy Robertson I'm creating a data table in R that has sub-rows, manually defined column names and a few columns hidden. According to the example provided in the DT manual https://rstudio.github.io/DT/002-rowdetails.html , this seems fairly straightforward (ie

dataframe to csv missing indexed column names

bib When writing the pandas mainTable dataframe mainTable.csv, but after writing to the file, the column names indexare missing . Why is this happening because I have specified index=True? mainTable.to_csv(r'/Users/myuser/Completed/mainTable.csv',index=True) m

Pandas: print column names, missing values

LinearLeopard: I am trying to print or get a list of column names with missing values. E.g data1 data2 data3 1 3 3 2 NaN 5 3 4 NaN I want to get ['data2', 'data3']. I wrote the following code: print('\n'.join(map( lambda x :

Subset a vector of column names by a specific sample prefix

Colin Lets say I have a dataframe that looks like this ca01<- c(1:10) ca02<- c(2:11) ca03<- c(3:12) stuff.1<- rep('test',10) other<- rep(9,10) data<- data.frame(ca01,ca02,ca03,stuff.1,other) Then, I create a vector containing the column names samps<- colname

Get column and row names of matrix indices in a vector

weight I have a 4x4 matrix and I want to identify elements in this matrix that are equal to a certain value (eg 1). I would like to save the indices of these elements along with the column and row names into two separate vectors. In the end I want to write all

Create vector from regular expression in column names

rise I have a dataframe with columns representing species. Species relatedness is encoded in the suffix of the column name: Ac_1234_ AnyString The string after the second underscore (_) indicates the affiliation of the species. I want to plot some networks bas

Subset a vector of column names by a specific sample prefix

Colin Lets say I have a dataframe that looks like this ca01<- c(1:10) ca02<- c(2:11) ca03<- c(3:12) stuff.1<- rep('test',10) other<- rep(9,10) data<- data.frame(ca01,ca02,ca03,stuff.1,other) Then, I create a vector containing the column names samps<- colname

Paste values from a vector to column names in R

Skampak I want to paste the values from the vector into the column names of the dataframe. Suppose the file has 4 columns and the tmp2 vector has 4 values. The for loop works just fine (the new colname is the value of tmp and the value of tmp2 together), but I

Delete column based on vector of names in R

Noruzian I have a data.framephone DATA. Using BASE R , I would like to know how to delete any variable DATAnamed any of the following : ar = c("out", "Name", "mdif" , "stder" , "mpre")? Currently, I use, DATA[ , !names(DATA) %in% ar]but while this removes unwa

Using column names from a vector in a for loop in dplyr

kd1978 This should be simple, but I'm struggling to get it to work. I currently have a vector of column names: columns <- c('product1', 'product2', 'product3', 'support4') I'm now trying to use dplyr in a for loop to change some columns, but I'm struggling to