0

I have issues getting the output I want from a data.frame. The structure of the data is the following:

head(test) A T L B E X D 4 no no no yes no no yes 7 no no no no no no no 11 no no no no no no no 12 no no no yes no no yes 17 no no no no no no no 27 no no no no no no no 

The output I get:

test[1,] A T L B E X D 4 no no no yes no no yes 

The output I want:

[1] "no" "no" "no" "yes" "no" "no" "yes" 

Or simply, the output I want is a vector where each element is a string value of that column in the df. I could do a for loop or some stupid thing like that, but I belive there should be a much simpler way that I am missing.

I have tried:

as.character(test[1,]) [1] "1" "1" "1" "2" "1" "1" "2" 

Not sure what I am missing here?

0

1 Answer 1

1

Use unlist and then as.character

as.character(unlist(test[1, ])) #[1] "no" "no" "no" "yes" "no" "no" "yes" 

test[1, ] is still a dataframe and applying as.character on data frame doesn't work. We use unlist to make dataframe to vector and then use as.character to convert it into character.

data

test <- structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "no", class = "factor"), T = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "no", class = "factor"), L = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "no", class = "factor"), B = structure(c(2L, 1L, 1L, 2L, 1L, 1L), .Label = c("no", "yes"), class = "factor"), E = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "no", class = "factor"), X = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "no", class = "factor"), D = structure(c(2L, 1L, 1L, 2L, 1L, 1L), .Label = c("no", "yes"), class = "factor")), class = "data.frame", row.names = c("4", "7", "11", "12", "17", "27")) 
Sign up to request clarification or add additional context in comments.

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.