将数据帧列拆分为多个具有列标题名称的文本文件

时间:2016-11-06 22:13:44

标签: r dataframe split export text-files

我有一个数据框 df ,大​​约有50列和20.000行。它看起来像下面的数据框:

Date                  P1               P2            P3            P4
1/1/2000               0               0.4           0             0
2/1/2000               0               0.1           0             0.1
3/1/2000               0.5             0             0             1
4/2000                 0.8             1.5           1             1

如何将每个(数字)列导出到文本文件?

Date 列除外,我可以从 df 删除/子集/删除)

我希望文本文件与列标题具有相同的名称。

P1.txt

0             
0              
0.5            
0.8            

P2.txt

0.4             
0.1              
0            
1.5            

这就是我到目前为止的50列:

     df$Date<-NULL
     for(i in c(1:32)){
     write.table(df[,i],file=paste0(names(df)[i],row.names = FALSE, col.names = FALSE, ".txt")) 

}

但生成的输出为: P1.txt

"P1"
"1"  0             
"2"  0              
"3"  0.5            
"4"  0.8  

是否有可能摆脱第一列&#34; 1&#34;,&#34; 2&#34;,&#34; 3&#34;,&#34; 4&#34;和标题&#34; P1&#34;?

2 个答案:

答案 0 :(得分:3)

<html>
<header>
<link rel="stylesheet" href="styles/style.css"/>
</header>
<body>
<div class="centered" id="content">
  <div class="row" id="r1">
    <div class="field empty" id="f1"></div>
    <div class="field empty" id="f4"></div>
    <div class="field empty" id="f7"></div>
  </div>
  <div class="row" id="r2">
    <div class="field empty" id="f2"></div>
    <div class="field empty" id="f5"></div>
    <div class="field empty" id="f8"></div>
  </div>
  <div class="row" id="r3">
    <div class="field empty" id="f3"></div>
    <div class="field empty" id="f6"></div>
    <div class="field empty" id="f9"></div>
  </div>
</div>
<script type="application/javascript" src="scripts/jquery/jquery-3.1.1.min.js"></script>
<script type="application/javascript" src="scripts/scripts.js"></script>         
</body>
</html>

这会在R的工作目录中创建V1.csv。

(l)应用这个创建V1.csv,V2.csv,...,V5.csv:

var changeTurn = false;
var turn = true;
var classCross = "cross";
var classCircle = "circle";
var classEmpty = "empty";    

$('.field').click(function(event) {
  event.stopImmediatePropagation();
  var div = $(this);
  changeTurn = false;
  if (turn) {
    if (fieldIsFree(div)) {
      div.removeClass(classEmpty);
      div.addClass(classCircle);
      changeTurn = true;
    }
  }
  else {
    if (fieldIsFree(div)) {
      div.removeClass(classEmpty);
      div.addClass(classCross);
      changeTurn = true;
    }
  }
  if (changeTurn)
    turn = !turn;
  return false;
});

function fieldIsFree(tmpdiv) {
  if (tmpdiv.hasClass(classCross))
    return false;
  if (tmpdiv.hasClass(classCircle))
    return false;
  return true;
}

现在,V1.csv,V2.csv,...,V5.csv在R的工作目录中创建,您可以通过# dummy data mydf <- as.data.frame(matrix(c(rnorm(20), rnorm(20), rnorm(20), rnorm(20), rnorm(20)), nrow=20)) V1 V2 V3 V4 V5 1 -0.62829066 1.15406529 -0.18567863 1.59192254 0.95744852 2 0.47275575 1.57982778 -0.78190000 0.56729278 -0.99929298 ... 20 0.30557789 0.55654245 0.43422811 0.22404488 -0.39800789 write.csv(mydf[, 1], file = paste0(names(mydf)[1], ".csv")) 找到。

上面的单行内容将lapply(1:5, function(x) write.csv(mydf[, x], file = paste0(names(mydf)[x], ".csv", row.names = FALSE, col.names = FALSE, quote = FALSE))) 打印到控制台,同时在R的工作目录中创建V1.csv,V2.csv,...,V5.csv。

答案 1 :(得分:0)

这就是我用作解决方案的方法:

### remove date column
df7$Date<-NULL

### loop writing text files, for 50 columns, without column and row names, and use the columnnames as text file names.  
for(i in c(1:50)){
write.table(df7[,i],row.names = FALSE, col.names = FALSE,file=paste0(names(df7)[i],".txt"))
}