我的输入文件从普通的csv表开始。
x <- read.table(textConnection(
+ ' models cores time
+ aa c1 xxx|yyy
+ aa c2 xxx|zzz
+ aa c3 www
+ aa c4 xxx|vvv
+ bb c1 vvv|www
+ bb c2 www|qqq
+ bb c3 xxx|uuu
+ bb c4 uuu' ), header=TRUE)
这是一个以因子为所有条目的文件,如下所示:
> str(x)
'data.frame': 8 obs. of 3 variables:
$ models: Factor w/ 2 levels "aa","bb": 1 1 1 1 2 2 2 2
$ cores : Factor w/ 4 levels "c1","c2","c3",..: 1 2 3 4 1 2 3 4
$ time : Factor w/ 8 levels "uuu","vvv|www",..: 7 8 3 6 2 4 5 1
为了使用命令“strsplit”拆分最后一列,我参考之前发布的问题完成了以下步骤。
> write.csv(x, file="x.csv")
> y <- read.csv(file="x.csv",header=TRUE,stringsAsFactors=FALSE)
> str(y)
'data.frame': 8 obs. of 4 variables:
$ X : int 1 2 3 4 5 6 7 8
$ models: chr "aa" "aa" "aa" "aa" ...
$ cores : chr "c1" "c2" "c3" "c4" ...
$ time : chr "xxx|yyy" "xxx|zzz" "www" "xxx|vvv" ...
Warning messages:
1: closing unused connection 4 (" models cores time \naa c1 xxx|yyy \naa c2 xxx|zzz \naa c3 www \naa c4 xxx|vvv \nbb c1 vvv|www \nbb c2 www|qqq \nbb c3 xxx|uuu \nbb c4 uuu")
2: closing unused connection 3 (" models cores time \n4 1 0.000365 \n4 2 0.000259 \n4 3 0.000239 \n4 4 0.000220 \n8 1 0.000259 \n8 2 0.000249 \n8 3 0.000251 \n8 4 0.000258")
> df2 <- as.data.frame(
+ t(
+ do.call(cbind,
+ lapply(1:nrow(y),function(x){
+ sapply(unlist(strsplit(y[x,4],"\\|")),c,y[x,2:3],USE.NAMES=FALSE)
+ }) ) ) )
> str(df2)
结果就是我所需要的。
> df2
V1 models cores
1 xxx aa c1
2 yyy aa c1
3 xxx aa c2
4 zzz aa c2
5 www aa c3
6 xxx aa c4
7 vvv aa c4
8 vvv bb c1
9 www bb c1
10 www bb c2
11 qqq bb c2
12 xxx bb c3
13 uuu bb c3
14 uuu bb c4
当我输入str(df2)时,我发现所有条目都是chr:
的列表'data.frame': 14 obs. of 3 variables:
$ V1 :List of 14
..$ : chr "xxx"...
$ models:List of 14
..$ : chr "aa"
..$ : chr "aa"
$ models:List of 14
..$ : chr "aa"
..$ : chr "aa"
但是,我很难再将这个最终结果保存为csv表。
> write.csv(df2, file="df2.csv")
Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec, as.integer(quote), :
unimplemented type 'list' in 'EncodeElement'
如何以csv格式再次保存df2文件?请帮助。
答案 0 :(得分:3)
你在做什么似乎非常愚蠢 - 为什么要把一些东西写到CSV只是为了再次读回来? - 但鉴于df2
大致你想要它,你需要unlist()
df2
中的三个组件并作为数据框投回。
out <- data.frame(lapply(df2, function(x) factor(unlist(x))))
这给了我们:
> out
V1 models cores
1 xxx aa c1
2 yyy aa c1
3 xxx aa c2
4 zzz aa c2
5 www aa c3
6 xxx aa c4
7 vvv aa c4
8 vvv bb c1
9 www bb c1
10 www bb c2
11 qqq bb c2
12 xxx bb c3
13 uuu bb c3
14 uuu bb c4
> str(out)
'data.frame': 14 obs. of 3 variables:
$ V1 : Factor w/ 7 levels "qqq","uuu","vvv",..: 5 6 5 7 4 5 3 3 4 4 ...
$ models: Factor w/ 2 levels "aa","bb": 1 1 1 1 1 1 1 2 2 2 ...
$ cores : Factor w/ 4 levels "c1","c2","c3",..: 1 1 2 2 3 4 4 1 1 2 ...
可以再读出来:
> write.csv(out, file="out.csv", row.names = FALSE)
> read.csv("out.csv")
V1 models cores
1 xxx aa c1
2 yyy aa c1
3 xxx aa c2
4 zzz aa c2
5 www aa c3
6 xxx aa c4
7 vvv aa c4
8 vvv bb c1
9 www bb c1
10 www bb c2
11 qqq bb c2
12 xxx bb c3
13 uuu bb c3
14 uuu bb c4
<强>更新强>
从x
直接转到所需的输出而不是将其读取到CSV并再次返回然后处理y
会更简单。例如,这从x
直接转到与上面out
相同的结果:
V1 <- with(x, strsplit(as.character(time), "\\|"))
lens <- lapply(V1, length)
out2 <- data.frame(V1 = factor(unlist(V1)),
models = with(x, rep(models, times = lens)),
cores = with(x, rep(cores, times = lens)))
给出了:
> out2
V1 models cores
1 xxx aa c1
2 yyy aa c1
3 xxx aa c2
4 zzz aa c2
5 www aa c3
6 xxx aa c4
7 vvv aa c4
8 vvv bb c1
9 www bb c1
10 www bb c2
11 qqq bb c2
12 xxx bb c3
13 uuu bb c3
14 uuu bb c4
> str(out2)
'data.frame': 14 obs. of 3 variables:
$ V1 : Factor w/ 7 levels "qqq","uuu","vvv",..: 5 6 5 7 4 5 3 3 4 4 ...
$ models: Factor w/ 2 levels "aa","bb": 1 1 1 1 1 1 1 2 2 2 ...
$ cores : Factor w/ 4 levels "c1","c2","c3",..: 1 1 2 2 3 4 4 1 1 2 ...
> all.equal(out, out2)
[1] TRUE
<强>除了:强>
顺便说一句,当您从R控制台复制代码时,我们不会轻易粘贴您的代码,因此它包含提示(+
)。相反,你可以完成dput(x)
并将其粘贴到你的Q:
structure(list(models = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("aa", "bb"), class = "factor"), cores = structure(c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("c1", "c2", "c3", "c4"
), class = "factor"), time = structure(c(7L, 8L, 3L, 6L, 2L,
4L, 5L, 1L), .Label = c("uuu", "vvv|www", "www", "www|qqq", "xxx|uuu",
"xxx|vvv", "xxx|yyy", "xxx|zzz"), class = "factor")), .Names = c("models",
"cores", "time"), class = "data.frame", row.names = c(NA, -8L
))
然后我们都可以完成:
x <- structure(list(models = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("aa", "bb"), class = "factor"), cores = structure(c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("c1", "c2", "c3", "c4"
), class = "factor"), time = structure(c(7L, 8L, 3L, 6L, 2L,
4L, 5L, 1L), .Label = c("uuu", "vvv|www", "www", "www|qqq", "xxx|uuu",
"xxx|vvv", "xxx|yyy", "xxx|zzz"), class = "factor")), .Names = c("models",
"cores", "time"), class = "data.frame", row.names = c(NA, -8L
))
与创建df2
的调用相同。这本来是更可取的:
write.csv(x, file="x.csv")
y <- read.csv(file="x.csv", header=TRUE, stringsAsFactors=FALSE)
df2 <- data.frame(
t(do.call(cbind,
lapply(1:nrow(y),function(x){
sapply(unlist(strsplit(y[x,4],"\\|")),c,y[x,2:3],
USE.NAMES=FALSE)
}))))
对我们来说,重建你拥有的对象和你尝试过的东西是一件简单的事情。
答案 1 :(得分:0)
fun_transform <- function(.x){
time_split <- strsplit(.x$time,split="\\|")
n_rec <- sapply(time_split,length)
ind <- rep(seq(nrow(.x)),n_rec)
cbind(.x[ind,1:2],time=unlist(time_split,use.names=FALSE))
}
df2 <- fun_transform(y)
编辑 - 示例数据
txt <- textConnection(
' models cores time
aa c1 xxx|yyy
aa c2 xxx|zzz
aa c3 www
aa c4 xxx|vvv
bb c1 vvv|www
bb c2 www|qqq
bb c3 xxx|uuu
bb c4 uuu' )
y <- read.table(txt, header=TRUE,as.is=TRUE)
close(txt)