R - 从data.frame中提取双精度矢量

时间:2014-10-04 16:53:07

标签: r class dataframe double character

我使用read.table()使用或不使用header=T来解决此问题,尝试使用data.frame从生成的as.double(as.character())中提取双精度矢量(请参阅?factor )。

但那只是如何我意识到我不理解R的逻辑。所以你不会看到例如以下代码中的read.table,仅包含必要的部分。你能告诉我以下选项之间的区别吗?

  1. 使用等效的header=T

    (a <- data.frame(array(c(0.5,0.5,0.5,0.5), c(1,4))))
    as.character(a)
    # [1] "0.5" "0.5" "0.5" "0.5"
    
  2. 没有header=T等价物:

    b <- data.frame(array(c("a",0.5,"b",0.5,"c",0.5,"d",0.5), c(2,4)))
    (a <- b[2,])
    as.character(a)
    # [1] "1" "1" "1" "1"
    
    (a <- data.frame(a, row.names=NULL)) # now there's not even a visual difference
    as.character(a)
    # [1] "1" "1" "1" "1"
    

1 个答案:

答案 0 :(得分:0)

问题在于data.frame的默认设置,其中一个选项stringsAsFactors设置为TRUE。这是您的方案中的问题,因为当您使用header = FALSE时,该行中存在的字符值会将整个列强制转换为字符,然后将其转换为因子(除非您设置stringsAsFactors = FALSE)。 / p>

以下是一些可以使用的示例:

## Two similar `data.frame`s -- just one argument different

b <- data.frame(array(c("a",0.5,"b",0.5,"c",0.5,"d",0.5), c(2,4)))
b2 <- data.frame(array(c("a",0.5,"b",0.5,"c",0.5,"d",0.5), c(2,4)),
                stringsAsFactors = FALSE)

## First with "b"

as.character(b[2, ])
# [1] "1" "1" "1" "1"

sapply(b[2, ], as.character)
#    X1    X2    X3    X4 
# "0.5" "0.5" "0.5" "0.5"
as.matrix(b)[2, ]
#    X1    X2    X3    X4 
# "0.5" "0.5" "0.5" "0.5"
as.double(as.matrix(b)[2, ])
# [1] 0.5 0.5 0.5 0.5

## Now with "b2"

as.character(b2[2, ])
# [1] "0.5" "0.5" "0.5" "0.5"
as.double(as.character(b2[2, ]))
# [1] 0.5 0.5 0.5 0.5