我需要将数据框的所有行转换为字符串。
这是一个示例数据:
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"
因此,我需要一个与源文件中的值完全相同的数据框。我做错了什么?