在一个y值中创建多个x值的数据集

时间:2018-04-06 04:11:15

标签: r dataset scatter-plot

我有一个如下所示的相关数据集:

V1    V2    R2    
 1    2    0.4    
 1    3    0.5    
 3    5    0.3    

我希望将它转换为两列数据,以便在一个y(在列R2中)中有多个x(在列V中)用于散点图。它看起来像这样:

V    R2    
1    0.4    
2    0.4        
1    0.5    
2    0.5    
3    0.5    
3    0.3    
4    0.3    
5    0.3    

我怎样才能在R?

中这样做

2 个答案:

答案 0 :(得分:2)

在tidyverse中,您可以使用app.get('/api/imagesearch/:query', newQuery) app.post('/', newQuery) function newQuery (req, res) { let query = req.body.query || req.params.query console.log(`Search Query: ${query}`) res.status(200) res.set('Content-Type', 'application/json') // This doesn't work let searchResults = JSON.stringify(cseSearch(req)) res.end(searchResults) } function cseSearch (request) { let cseParams = '' + `?q=${request.params.query}` + `&cx=${process.env.CSE_ID}` + `&key=${process.env.API_KEY}` + '&num=10' + '&safe=high' + '&searchType=image' + `&start=${request.query.offset || 1}` let options = { hostname: 'www.googleapis.com', path: '/customsearch/v1' + encodeURI(cseParams) } let cseRequest = https.request(options, cseResponse => { let jsonString = '' let searchResults = [] cseResponse.on('data', data => { jsonString += data }) cseResponse.on('end', () => { let cseResult = JSON.parse(jsonString) let items = cseResult.items items.map(item => { let resultItem = { url: item.link, snippet: item.title, thumbnail: item.image.thumbnailLink, context: item.image.contextLink } searchResults.push(resultItem) }) // This doesn't work... wrong scope, two callbacks deep return searchResults }) }) cseRequest.on('error', e => { console.log(e) }) cseRequest.end() } 制作所需向量的列表列,以便在每对起点和终点上迭代purrr::map2,然后使用seq展开:< / p>

tidyr::unnest

在基数R中,没有简单的等效df <- data.frame(V1 = c(1L, 1L, 3L), V2 = c(2L, 3L, 5L), R2 = c(0.4, 0.5, 0.3)) library(tidyverse) df %>% transmute(V = map2(V1, V2, seq), R2) %>% unnest() #> R2 V #> 1 0.4 1 #> 2 0.4 2 #> 3 0.5 1 #> 4 0.5 2 #> 5 0.5 3 #> 6 0.3 3 #> 7 0.3 4 #> 8 0.3 5 ,因此更容易使用unnest(多变量Map,大致相当于lapply以上)构建一个数据框列表,其中包含purrr::map2值(由R2回收),然后将data.frame编入单个数据框:

do.call(rbind, ...)

查看每种产品的中间产品,以了解它们的工作原理。

答案 1 :(得分:0)

以下是使用public function getCsv() { $shipmentIds = $this->getRequest()->getParam('internal_shipment_ids'); // Parameter will be like internal_'checkboxName' $csv = ''; $this->_isExport = true; $this->_prepareGrid(); if($shipmentIds) { $shipmentIds = explode(',' , $shipmentIds); $this->getCollection()->addAttributeToFilter('main_table.entity_id', array('in' => $shipmentIds))->getSelect()->limit(); $this->getCollection()->setPageSize(0); $this->getCollection()->load(); $this->_afterLoadCollection(); } else { $this->getCollection()->getSelect()->limit(); $this->getCollection()->setPageSize(0); $this->getCollection()->load(); $this->_afterLoadCollection(); } $data = array(); foreach ($this->_columns as $column) { if (!$column->getIsSystem()) { $data[] = '"'.$column->getExportHeader().'"'; } } $csv.= implode(',', $data)."\n"; foreach ($this->getCollection() as $item) { $data = array(); foreach ($this->_columns as $column) { if (!$column->getIsSystem()) { $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($item)) . '"'; } } $csv.= implode(',', $data)."\n"; } if ($this->getCountTotals()) { $data = array(); foreach ($this->_columns as $column) { if (!$column->getIsSystem()) { $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($this->getTotals())) . '"'; } } $csv.= implode(',', $data)."\n"; } return $csv; }

的一个选项
data.table