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?

3
  • 3
    data.frame(..., stringsAsFactors = FALSE) Commented Jun 10, 2014 at 19:09
  • 3
    Possible duplicate: every R question ever Commented Jun 10, 2014 at 19:13
  • The linked "duplicate" question is quite different from this one. This is asking how to not PRINT factors, the other is asking how to remove extra factor levels that are no longer used. Commented Jul 5, 2019 at 14:59

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

This suggestion worked for me when printing a factor vector.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.