3

Part of my code is similar to following:

id.row <- c("x1","x2", "x10", "x20")
id.num <- c(1, 2, 10, 20)
id.name <- c("one","two","ten","twenty")
info.data <- data.frame(id.row, id.num, id.name)

req.mat <- matrix(1:4, nrow = 2, ncol = 2)
row.names(req.mat) <- c("x1","x10")

p1 <- info.data$id.row %in% row.names(req.mat)

op1 <- info.data$id.num[p1]
op2 <- info.data$id.name[p1]

I think the code is pretty much self explanatory and am getting the results that i want. There is no problem in printing op1 but when I am trying to get op2 its giving me additional output (Levels). As,there are 1000s of rows in original info.data so this "level" is not looking nice. Is there a way to turn it off?

alistaire
  • 42,459
  • 4
  • 77
  • 117
Polar.Ice
  • 138
  • 2
  • 12

2 Answers2

4

Maybe you can use print(op2, max.levels=0)

Uwe
  • 41,420
  • 11
  • 90
  • 134
Pablo
  • 140
  • 1
  • 11
0

If you didn't want any of your variables to be factors, then

info.data <- data.frame(id.row, id.num, id.name, stringsAsFactors=F)

is a good choice. If you wanted id.row but not id.name to be a factor then

info.data <- data.frame(id.row=factor(id.row), id.num, id.name, 
    stringsAsFactors=F)

is better to set that up. If you created your data in some other way so they already exist as factors in the data.frame, you can convert them back to character with

info.data$id.name <- as.character(info.data$id.name)

If you do want id.name to be a factor but just want to drop the extra levels after subsetting, then

op2 <- info.data$id.name[p1, drop=T]

will do the trick.

MrFlick
  • 195,160
  • 17
  • 277
  • 295