我需要将数据帧的所有行转换为字符串。
以下是一个示例数据:
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"
因此,我需要一个数据框,其值与源文件中的值完全相同。我做错了什么?
答案 0 :(得分:3)
您可以在colClasses
中设置read.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"
答案 1 :(得分:2)
执行此操作的最佳方法是首先以字符形式读取数据。您可以使用read_table的colClasses
参数执行此操作:
td <- read.table("test.csv", sep=',', colClasses="character")