写入和读取csv文件后,R interaction()被截断

时间:2015-10-14 15:24:28

标签: r

我使用interaction()函数创建新变量并组合“Study”号(例如1,2,3)和“Subject”号(例如1:20)。当我将数据帧写入csv并在另一个脚本中读取时,此变量以下列方式更改: 1.10转换为1.1或1.1转换为1.10和 2.10转换为2.1或2.1转换为2.10

这也发生在我写的这个简单的代码中:

x <- c(1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2)
y <- 1:15

xy <- as.data.frame(cbind(x,y))
xy$xy <- interaction(x,y)

xy

write.csv(xy, "xy.csv")
xy2 <- read.csv("xy.csv")

xy2

有没有办法轻松解决?

谢谢!

1 个答案:

答案 0 :(得分:3)

您可以在列中明确读取字符而不是让R猜测数据类型,因为它看起来非常像十进制数

<html>
<head>
<title>Information</title>
   <script  src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
   <script src="http://code.highcharts.com/highcharts.js"></script>  
   <script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="width: 800px; height: 500px; margin: 0 auto">    </div>
<script language="JavaScript">


$(document).ready(function() {  
   var chart = {
      type: 'bar'
   };
   var title = {
      text: 'Activities of the Elderly'   
   };
   var subtitle = {
      text: 'Testing'  
   };
   var xAxis = {
      categories: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
      title: {
         text: null
      }
   };
   var yAxis = {
      min: 0,
      title: {
         text: 'No. of times movement detected',
         align: 'high'
      },
      labels: {
         overflow: 'justify'
      }
   };
   var tooltip = {
      valueSuffix: 'times'
   };
   var plotOptions = {
      bar: {
         dataLabels: {
            enabled: true
           }
      }
   };

// Load the fonts
Highcharts.createElement('link', {
   href: '//fonts.googleapis.com/css?family=Signika:400,700',
   rel: 'stylesheet',
   type: 'text/css'
}, null, document.getElementsByTagName('head')[0]);

// Add the background image to the container
Highcharts.wrap(Highcharts.Chart.prototype, 'getContainer', function (proceed) {
   proceed.call(this);
   this.container.style.background = 'url(http://www.highcharts.com/samples/graphics/sand.png)';
});


Highcharts.theme = {
   colors: ["#f45b5b", "#8085e9", "#8d4654", "#7798BF", "#aaeeee", "#ff0066", "#eeaaee",
  "#55BF3B", "#DF5353", "#7798BF", "#aaeeee"],
   chart: {
      backgroundColor: null,
      style: {
         fontFamily: "Signika, serif"
      }
   },
   title: {
      style: {
         color: 'black',
         fontSize: '16px',
         fontWeight: 'bold'
      }    
   },
   subtitle: {
    style: {
         color: 'black'
      }
   },
   tooltip: {
      borderWidth: 0
   },
   legend: {
      itemStyle: {
         fontWeight: 'bold',
         fontSize: '13px'
      }
   },
   xAxis: {
      labels: {
         style: {
            color: '#6e6e70'
         }
      }
   },
   yAxis: {
      labels: {
         style: {
            color: '#6e6e70'
         }
      }
   },
   plotOptions: {
      series: {
         shadow: true
      },
      candlestick: {
         lineColor: '#404048'
      },
      map: {
         shadow: false
      }
   },

   // Highstock specific
   navigator: {
      xAxis: {
         gridLineColor: '#D0D0D8'
      }
   },
   rangeSelector: {
      buttonTheme: {
         fill: 'white',
         stroke: '#C0C0C8',
     'stroke-width': 1,
         states: {
            select: {
               fill: '#D0D0D8'
            }
         }
      }
   },
   scrollbar: {
      trackBorderColor: '#C0C0C8'
   },

   // General
   background2: '#E0E0E8'

};

// Apply the theme
Highcharts.setOptions(Highcharts.theme);


<?php
mysql_connect ("server","user","password") or die ('Cannot connect to MySQL: ' . mysql_error());
mysql_select_db ("dbtable") or die ('Cannot connect to the database: ' . mysql_error());

$result = mysql_query("SELECT * from sensor") or die ('Query is invalid: ' .   mysql_error());

while ($r = mysql_fetch_array($result)) {
   $date['data'][] = $r['date'];
   $value['data'][] = $r['value'];

}
$result = array();
array_push($result,$date);
array_push($result,$value);

echo json_encode($result); 
?>


   var legend = {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'top',
      x: -40,
      y: 100,
      floating: true,
      borderWidth: 1,
      backgroundColor: ((Highcharts.theme &&         Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
      shadow: true
   };
   var credits = {
      enabled: false
   };

   var series= [{
            name: 'Kitchen',
            data: [107, 31, 635, 203, 2]
        }, {
            name: 'Toilet',
            data: [133, 156, 947, 408, 6]
        }, {
            name: 'LivingRoom',
            data: [973, 914, 404, 732, 34]      
        }, {
            name: 'BedRoom',
            data: [973, 914, 404, 732, 34]
    }
   ]; 



   var json = {};   
   json.chart = chart; 
   json.title = title;   
   json.subtitle = subtitle; 
   json.tooltip = tooltip;
   json.xAxis = xAxis;
   json.yAxis = yAxis;  
   json.series = series;
   json.plotOptions = plotOptions;
   json.legend = legend;
   json.credits = credits;
   $('#container').highcharts(json);

});
</script>
</body>
</html> 

您还可以更改用于交互的分隔符,使其看起来不像十进制数

write.csv(xy, "xy.csv", row.names=FALSE)
xy2 <- read.csv("xy.csv", colClasses=c("numeric","numeric","character"))