你的问题并不完全清楚。例如,
- 您的文件中是否有数字 [1]、[2]、...?
- 偶数行只是奇数行的小写版本吗?
忽略数字并假设奇数行和偶数行不同,一种解决方案是:
##Read in the data.
tmp = read.table(textConnection("/tI /tam /tCharlotte
/ti /tam /tcharlotte
/tYou /tare /tsmart
/tyou /tare /tsmart"), sep="\n", stringsAsFactors=FALSE)
##Take the odd rows
##gsub: remove white space
##strsplit: split the string on "\t"
##unlist: go from a list to a vector
c1 = unlist(
strsplit(
gsub(" ", "", tmp[seq(1,nrow(tmp), 2),]), "/t"))
##Ditto the even rows
c2 = unlist(
strsplit(
gsub(" ", "", tmp[seq(2,nrow(tmp), 2),]), "/t"))
这为我们提供了两个可以放入数据框中的向量:
dd = data.frame(c1 = c1, c2 = c2)
我想你不想要空行,所以只需删除它们:
dd[apply(dd, 1, function(i) sum(nchar(i))>0),]