Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Turning a data frame row into a character vector

Short version:

I do not understand the behaviour of as.character when trying to convert a single row of a data frame to a character vector.

> mydf <- data.frame("myvar1"=c("mystring","2"),"myvar2"=c("mystring","3"))
> mydf # nice!
myvar1   myvar2
1 mystring mystring
2        2        3
> as.character(mydf[1,])
[1] "2" "2"
> as.character(as.vector(mydf[1,]) ) 
[1] "2" "2"

Maybe somebody could give me an explanation for the last 2 output lines and the correct approach? Thanks a lot.

Background/Purpose:

I want to use lre() in order to detect consecutive occurrences of values in a row of a data frame (with columns of different data types).

Problem: lre() requires a vector, vectors require a definite data type (integer, character, factor, ...). My idea here is to turn the data frame row into a character vector to avoid data loss through conversion.

like image 279
nilsole Avatar asked Jun 30 '14 14:06

nilsole


People also ask

How to create a character vector in R using data frame?

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame values to create a character vector then as.character function can be used. For example, if we have a data frame df then all the values in the df can form a character vector using as.character (df []).

How to convert a one-row data frame to a vector in R?

In order to convert this one-row data frame to a vector, we can use the as.numeric function as follows: The final output is a numeric vector consisting of the values of row three.

How to create a vector from a Dataframe?

A data frame will often have different data types in each column that need to be coerced to character strings. Even after coercing the columns to character format, the data.frame "shell" needs to stripped-off to create a vector via a command like unlist. With a combination of dplyr and base R this can be done in two lines.

How do I convert a row to a vector?

If you know the mode of the whole row, or can convert to the same type, you can use the mode's conversion function (for example, as.numeric ()) to convert to a vector. For example:


1 Answers

Your data frame columns aren't characters they are factors.

When you create a data frame the default is that characters are factors. You can see this clearly if you select a column

R> mydf[,1]
[1] mystring 2       
Levels: 2 mystring

To avoid this behaviour set the stringsAsFactors argument to FALSE

mydf = data.frame("myvar1"=c("mystring", "2"),
                    "myvar2"=c("mystring", "3"), 
                     stringsAsFactors=FALSE)

You should also look at this question: How to convert a data frame column to numeric type?

like image 68
csgillespie Avatar answered Oct 22 '22 08:10

csgillespie