f******9 发帖数: 267 | 1 When I input a file containing:
ABCDE
ABCDE
ABCDE
the row length is 1 from the R output.
Actually, there are 5 columns, so the row length should be 5. What should I
do to let R know the dimension of
this data.frame is (3,5) instead of (3,1)? | s*****n 发帖数: 2174 | 2 are there any field delimiters?
If there are no delimiters, R will treat these as string naturally. | f******9 发帖数: 267 | 3
There are not any field delimiters. How to let R treat them individually
instead of string?
【在 s*****n 的大作中提到】 : are there any field delimiters? : If there are no delimiters, R will treat these as string naturally.
| s*****n 发帖数: 2174 | 4 what you can do is to read them as a string into R, and then split the
string into each characters.
strsplit("ABCDE", split = "")
[[1]]
[1] "A" "B" "C" "D" "E" | f******9 发帖数: 267 | 5 If I use this command, all the rows will be connected together.
My file contains more than 100 such rows and how can I keep each row the
same length as the original. If [[1]]
doesn't exist in the output, that will be much easier.
Thanks!
【在 s*****n 的大作中提到】 : what you can do is to read them as a string into R, and then split the : string into each characters. : strsplit("ABCDE", split = "") : [[1]] : [1] "A" "B" "C" "D" "E"
| s*****n 发帖数: 2174 | 6 I just give you a hint, of course you need to modify it to fit what you need
. for example
> data
V1
1 ABCDE
2 ABCDE
3 ABCDE
> t(sapply(1:dim(data)[1], function(i) unlist(strsplit(data$V1[i], split = "
"))))
[,1] [,2] [,3] [,4] [,5]
[1,] "A" "B" "C" "D" "E"
[2,] "A" "B" "C" "D" "E"
[3,] "A" "B" "C" "D" "E" | f******9 发帖数: 267 | 7 Thanks a lot!
need
"
【在 s*****n 的大作中提到】 : I just give you a hint, of course you need to modify it to fit what you need : . for example : > data : V1 : 1 ABCDE : 2 ABCDE : 3 ABCDE : > t(sapply(1:dim(data)[1], function(i) unlist(strsplit(data$V1[i], split = " : ")))) : [,1] [,2] [,3] [,4] [,5]
| t**i 发帖数: 688 | 8 read.table(...,sep="")
I
【在 f******9 的大作中提到】 : When I input a file containing: : ABCDE : ABCDE : ABCDE : the row length is 1 from the R output. : Actually, there are 5 columns, so the row length should be 5. What should I : do to let R know the dimension of : this data.frame is (3,5) instead of (3,1)?
| s*****n 发帖数: 2174 | 9 this doesn't work.
sep = "" 本来就是默认选项了.
【在 t**i 的大作中提到】 : read.table(...,sep="") : : I
| c*****m 发帖数: 4817 | 10 if the width is fixed, then you can use read.fwf, for example
> data1 = read.fwf("M:/test.txt", widths=c(1,1,1,1,1))
> data1
V1 V2 V3 V4 V5
1 A B C D E
2 A B C D E
3 A B C D E
I
【在 f******9 的大作中提到】 : When I input a file containing: : ABCDE : ABCDE : ABCDE : the row length is 1 from the R output. : Actually, there are 5 columns, so the row length should be 5. What should I : do to let R know the dimension of : this data.frame is (3,5) instead of (3,1)?
| s*****n 发帖数: 2174 | 11 this is really nice. I didn't know this before.
【在 c*****m 的大作中提到】 : if the width is fixed, then you can use read.fwf, for example : > data1 = read.fwf("M:/test.txt", widths=c(1,1,1,1,1)) : > data1 : : V1 V2 V3 V4 V5 : 1 A B C D E : 2 A B C D E : 3 A B C D E : : I
|
|