2

我需要将数据框的所有行转换为字符串。

这是一个示例数据:

1.12331,4.331123,4.12335435,1,"asd"
1.123453345,5.654456,4.889999,1.45456,"qwe"
2.00098,5.5445,4.768799,1.999999,"ttre"

我将这些数据读入 R,得到了一个数据框。

td<-read.table("test.csv", sep=',')

当我运行apply(td, 2, as.character)这些数据时,我得到了

    V1       V2       V3       V4       V5    
[1,] "1.1233" "4.3311" "4.1234" "1.0000" "asd" 
[2,] "1.1235" "5.6545" "4.8900" "1.4546" "qwe" 
[3,] "2.0010" "5.5445" "4.7688" "2.0000" "ttre"

但是当我只在数字列上做同样的事情时,我得到了不同的结果:

apply(td[,1:4], 2, as.character)

     V1            V2         V3           V4        
[1,] "1.12331"     "4.331123" "4.12335435" "1"       
[2,] "1.123453345" "5.654456" "4.889999"   "1.45456" 
[3,] "2.00098"     "5.5445"   "4.768799"   "1.999999"

因此,我需要一个与源文件中的值完全相同的数据框。我做错了什么?

4

2 回答 2

4

您可以设置colClassesread.table()将所有列设置为character.

 td <- read.table("test.csv", sep=',',colClasses="character")
 td
           V1       V2         V3       V4   V5
1     1.12331 4.331123 4.12335435        1  asd
2 1.123453345 5.654456   4.889999  1.45456  qwe
3     2.00098   5.5445   4.768799 1.999999 ttre

 str(td)
'data.frame':   3 obs. of  5 variables:
 $ V1: chr  "1.12331" "1.123453345" "2.00098"
 $ V2: chr  "4.331123" "5.654456" "5.5445"
 $ V3: chr  "4.12335435" "4.889999" "4.768799"
 $ V4: chr  "1" "1.45456" "1.999999"
 $ V5: chr  "asd" "qwe" "ttre"
于 2013-02-12T11:11:35.110 回答
2

最好的方法是首先将数据作为字符读取。您可以使用colClassesread.table 的参数来执行此操作:

td <- read.table("test.csv", sep=',', colClasses="character")
于 2013-02-12T11:11:26.213 回答