Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retain improper name with indexing

Tags:

r

I have need to name columns of a data.frame with duplicate names. inside of data.frame you can use check.names = FALSE to do the naughty name deed. But if you index this then you lose the naughty names when indexing. I want to retain those names. So beloe is an example and the output I get and I'd like to get:

x <- data.frame(b= 4:6, a =6:8, a =6:8, check.names = FALSE)
x[, -1]

I get:

  a a.1
1 6   6
2 7   7
3 8   8

I'd like:

  a a
1 6 6
2 7 7
3 8 8
like image 856
Tyler Rinker Avatar asked Dec 11 '22 21:12

Tyler Rinker


1 Answers

How about this:

subdf <- function(df, ii) {
    do.call("data.frame", c(as.list(df)[ii], check.names=FALSE))
}

subdf(x, -1)
#   a a
# 1 6 6
# 2 7 7
# 3 8 8

subdf(x, 2:3)
#   a a
# 1 6 6
# 2 7 7
# 3 8 8
like image 77
Josh O'Brien Avatar answered Jan 03 '23 02:01

Josh O'Brien