R Dataframe 中的级别

发布于 2024-10-05 03:34:13 字数 114 浏览 1 评论 0原文

我从 .csv 文件导入数据,并附加数据集。
我的问题:一个变量是整数形式,有 295 个级别。我需要使用这个变量来创建其他变量,但我不知道如何处理这些级别。

这些是什么?我该如何处理它们?

I imported data from a .csv file, and attached the dataset.
My problem: one variable is in integer form and has 295 levels. I need to use this variable to create others, but I don't know how to deal with the levels.

What are these, and how do I deal with them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠佳期 2024-10-12 03:34:44

进行摘要(数据)以检查是否正确读取内容。如果列不是应该是数字的,请查看 read.csv 的 colClasses 参数来强制它,这也可能会导致格式不良的数字产生 NA 值。

help(read.csv) 会有所帮助。

Do summary(data) to check things got read in properly. If columns aren't numeric that should be, look at the colClasses argument to read.csv to force it, which will probably also result in NA values for poorly-formed numbers.

help(read.csv) will help.

半窗疏影 2024-10-12 03:34:39

根据您的澄清,我建议您使用 read.table 和 header=TRUE、stringAsFactors=FALSE 和 as.is = !stringsAsFactors 和 sep="," 重做读取语句:

datinp <- read.table("Rdata.csv", header=TRUE, stringAsFactors=FALSE , 
                       as.is = !stringsAsFactors , sep=",") 
datinp$a <- as.numeric(datinp$a)
datinp$b <- as.numeric(datinp$b)
datinp$ctr <- with(datinp, as.integer(a/b) ) # no loop needed when using vector arithmetic

Working from your clarification I suggest you redo your read statement with read.table and header=TRUE, stringAsFactors=FALSE and as.is = !stringsAsFactors and sep=",":

datinp <- read.table("Rdata.csv", header=TRUE, stringAsFactors=FALSE , 
                       as.is = !stringsAsFactors , sep=",") 
datinp$a <- as.numeric(datinp$a)
datinp$b <- as.numeric(datinp$b)
datinp$ctr <- with(datinp, as.integer(a/b) ) # no loop needed when using vector arithmetic
婴鹅 2024-10-12 03:34:32

或者您可以简单地使用

d$x2 = as.numeric(as.character(d$x))

or you can simply use

d$x2 = as.numeric(as.character(d$x)).

幸福%小乖 2024-10-12 03:34:27

当您使用 read.table (或 read.csv? - 您没有指定)读取数据时,请添加参数 stringsAsFactors = FALSE。然后你将获得字符数据。

如果您期望该列为整数,那么您必须拥有无法解释为整数的数据,因此在读取后将其转换为数字。

txt <- c("x,y,z", "1,2,3", "a,b,c")

d <- read.csv(textConnection(txt))
sapply(d, class)
       x        y        z 
##"factor" "factor" "factor" 

## we don't want factors, but characters
d <- read.csv(textConnection(txt), stringsAsFactors = FALSE)
sapply(d, class)

#          x           y           z 
#"character" "character" "character" 

## convert x to numeric, and wear NAs for non numeric data
as.numeric(d$x)

#[1]  1 NA
#Warning message:
#NAs introduced by coercion 

最后,如果您想忽略这些输入详细信息并从因子中提取整数级别,请使用例如 as.numeric(levels(d$x))[d$x],按照 ?factor 中的“警告”。

When you read in the data with read.table (or read.csv? - you didn't specify), add the argument stringsAsFactors = FALSE. Then you will get character data instead.

If you are expecting integers for the column then you must have data that is not interpretable as integers, so convert to numeric after you've read it.

txt <- c("x,y,z", "1,2,3", "a,b,c")

d <- read.csv(textConnection(txt))
sapply(d, class)
       x        y        z 
##"factor" "factor" "factor" 

## we don't want factors, but characters
d <- read.csv(textConnection(txt), stringsAsFactors = FALSE)
sapply(d, class)

#          x           y           z 
#"character" "character" "character" 

## convert x to numeric, and wear NAs for non numeric data
as.numeric(d$x)

#[1]  1 NA
#Warning message:
#NAs introduced by coercion 

Finally, if you want to ignore these input details and extract the integer levels from the factor use e.g. as.numeric(levels(d$x))[d$x], as per "Warning" in ?factor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文