I read a csv in R and ran into a problem where it creates a bunch of columns after the data as shown in the picture.
How could I remove these columns as there seems to be over 100 of them
CodePudding user response:
you can try to select the first three columns after reading the file:
df = df[,c(1:3)]
CodePudding user response:
Ideally this should be solved while reading the data but we don't exactly know why those empty columns are coming.
So we can keep the columns which have at least 1 value which is not empty.
df <- df[colSums(df != '') > 0]
Or with dplyr
-
library(dplyr)
df <- df %>% select(where(~any(. != '')))