string R:将数据框行转换为字符向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24492426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:19:17  来源:igfitidea点击:

R: Turning a data frame row into a character vector

stringrvectortype-conversion

提问by nilsole

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.

尝试将数据帧的单行转换为字符向量时,我不明白 as.character 的行为。

> 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.

也许有人可以给我解释最后 2 行输出和正确的方法?非常感谢。

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).

我想使用lre()它来检测数据框一行中连续出现的值(具有不同数据类型的列)。

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.

问题:lre()需要一个向量,向量需要一个明确的数据类型(整数、字符、因子等)。我这里的想法是将数据帧行转换为字符向量,以避免通过转换丢失数据。

采纳答案by csgillespie

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 stringsAsFactorsargument to FALSE

为了避免这种行为,将stringsAsFactors参数设置为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?

您还应该看看这个问题:如何将数据框列转换为数字类型?

回答by coffeinjunky

Try this:

尝试这个:

 mydf <- data.frame("myvar1"=c("mystring","2"),"myvar2"=c("mystring","3"), stringsAsFactors=F)
 as.character(mydf[1,])
 [1] "mystring" "mystring"

Your strings have been coerced into factors, and you have been shown the factor levels.

您的字符串已被强制转换为因子,并且已向您显示因子级别。