Home > Software design >  R how to create a dataframe by adding columns
R how to create a dataframe by adding columns

Time:10-30

I am very very new to R....I have been using Python and MATLAB my whole life.

So here is what I would like to do. During each loop, I compute a column that I would like to add on to a dataframe.

Problem is that I do not know the length of the column. So I cannot create the dataframe to a specific length. So I keep getting an error when I try to add the column to the empty original empty dataframe...

# extract the data where the column 7 has no data. 
df_glm <- data.frame(matrix(ncol = 11, nrow = 0))
for (j in 1:ncol(data_cancer)){
  col_ele <- data_cancer[,j]
  col_filtered <- col_ele[col_bool7]
  # make new dataframe by concetenating the filtered column.
  df_glm[,i] <- col_filtered
}
data_cancer_filter <- data_cancer[,col_bool7]

How can I resolve this issue?

I am getting an error at df_glm[,i] because the column is as long as col_bool7. But I want to learn how to do this without creating dataframe of exact size beforehand.

CodePudding user response:

If I am understanding this correctly, you're looping through columns and taking the rows where col_bool7 is TRUE and putting it in another dataframe. dplyr filter() would be an efficient solution:

library(dplyr)

df_glm = data_cancer %>%
    filter(col_bool7) 
  • Related