Wednesday, September 22, 2010

Deleting columns in a data.frame in R

To delete a single column: 
data["colname"] <-NULL
data <- data[, -grep("colname",names(data))]

To delete multiple columns:

# by column number (e.g. columns with indices c1, c2 and c3 )
data <- data[,-c(c1,c2,c3)] 

# by column name:
data <- data[-match(c("var1","var2"), names(data))]
data <- data[-which(names(data) %in% c("muc3", "muc4"))]
or
data[c("var1","var2")] = list()


# all columns with the same root (e.g. variables "time1", "time2", ..., "timeN")
data <- data[-grep("time",names(data))]


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Followers

Blog Archive

google analytics