我需要构造一组变量,其中应基于2个部分来构造变量:a)名称b)一个以值[1]递增的数字。对于数量的增加,我使用一个for循环。我设法创建一个字符串,参见test1,但没有增加变量名,参见test2。
鉴于下面提供的错误代码,我假设R不想让我使用作为变量名称一部分的“ paste0”构造某些东西。
我的R码:
“ test1”的结果:
numbers_for_variable_name <- c(1,2,3)
# Test-1 [works]
# Construct string with increasing number.
for (i in numbers_for_variable_name) {
cat(paste0("number-", i, "\n"))
}
# Test-2 [does not work]
# Construct variable name with increasing number.
for (i in numbers_for_variable_name) {
paste0("number-", i) <- "p1"
}
我为test2遇到的错误是:
number-1
number-2
number-3
“ test2”的预期结果是:
Error in paste0("number-", i) <- "p1" :
target of assignment expands to non-language object
答案 0 :(得分:3)
您应该使用R附带的结构-列表。您可以使用lapply
或lapply
对其进行命名,轻松地对其进行子集化或对其应用功能(或仅遍历)。
numbers_for_variable_name <- c(1,2,3)
myresult <- vector("list", length = length(numbers_for_variable_name))
names(myresult) <- paste("number-", numbers_for_variable_name, sep = "")
for (i in numbers_for_variable_name) {
myresult[[i]] <- i
}
> myresult
$`number-1`
[1] 1
$`number-2`
[1] 2
$`number-3`
[1] 3
子设置:
> myresult[["number-3"]]
[1] 3
将函数应用于所有列表元素:
> lapply(myresult, FUN = function(x) x^2)
$`number-1`
[1] 1
$`number-2`
[1] 4
$`number-3`
[1] 9
答案 1 :(得分:2)
要使OP的代码正常工作,应为#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>
using ByteVec = std::vector<uint8_t>;
template<typename T, size_t size = sizeof(T)>
auto getReverseEndianValue(const auto & iter) {
union {
T result;
char tmp[size];
} buffer;
auto reverseIter = std::make_reverse_iterator(std::next(iter, size));
std::copy(reverseIter, std::next(reverseIter, size), buffer.tmp);
return buffer.result;
}
enum Edition {
Edition_Unknown = -1,
Edition_GRIB1 = 1,
};
namespace section {
class IS {
public:
uint32_t magicFlag;
uint32_t size;
Edition edition;
static IS read(const auto & iter) {
IS result;
result.magicFlag = getReverseEndianValue<uint32_t>(iter);
result.size = getReverseEndianValue<uint32_t, 3>(iter + 4);
result.edition = (*(iter + 7) == 1 ? Edition_GRIB1 : Edition_Unknown);
return result;
}
};
class PDS {
public:
uint32_t size;
uint8_t tableVersion;
uint8_t indentificatorOfCenter;
uint8_t numProcessID;
uint8_t gridIndentification;
uint8_t flagForGDSorBMS;
uint8_t indParamAndUnit;
uint8_t indTypeOfLevelOrLayer;
uint16_t levelOrLayer;
uint8_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t forecastTimeUnit;
uint8_t p1;
uint8_t p2;
uint8_t indTimeRange;
uint16_t averageOrAccumulate;
uint8_t missing;
uint8_t century;
uint8_t subcenterId;
uint16_t decimalScale;
ByteVec data;
static PDS read(const auto& iter) {
PDS result;
result.size = getReverseEndianValue<uint32_t, 3>(iter);
result.tableVersion = getReverseEndianValue<uint8_t>(iter + 3);
result.indentificatorOfCenter = getReverseEndianValue<uint8_t>(iter + 4);
result.numProcessID = getReverseEndianValue<uint8_t>(iter + 5);
result.gridIndentification = getReverseEndianValue<uint8_t>(iter + 6);
result.flagForGDSorBMS = getReverseEndianValue<uint8_t>(iter + 7);
result.indParamAndUnit = getReverseEndianValue<uint8_t>(iter + 8);
result.indTypeOfLevelOrLayer = getReverseEndianValue<uint8_t>(iter + 9);
result.levelOrLayer = getReverseEndianValue<uint16_t>(iter + 10);
result.year = getReverseEndianValue<uint8_t>(iter + 12);
result.month = getReverseEndianValue<uint8_t>(iter + 13);
result.day = getReverseEndianValue<uint8_t>(iter + 14);
result.hour = getReverseEndianValue<uint8_t>(iter + 15);
result.minute = getReverseEndianValue<uint8_t>(iter + 16);
result.forecastTimeUnit = getReverseEndianValue<uint8_t>(iter + 17);
result.p1 = getReverseEndianValue<uint8_t>(iter + 18);
result.p2 = getReverseEndianValue<uint8_t>(iter + 19);
result.indTimeRange = getReverseEndianValue<uint8_t>(iter + 20);
result.averageOrAccumulate = getReverseEndianValue<uint16_t>(iter + 21);
result.missing = getReverseEndianValue<uint8_t>(iter + 23);
result.century = getReverseEndianValue<uint8_t>(iter + 24);
result.subcenterId = getReverseEndianValue<uint8_t>(iter + 25);
result.decimalScale = getReverseEndianValue<uint16_t>(iter + 26);
return result;
}
};
}
class GribData {
private:
section::IS secIS;
section::PDS secPDS;
public:
void print() {
std::cout
<< "### Section IS ###\n"
<< "magicFlag: " << +secIS.magicFlag << "\n"
<< "size: " << +secIS.size << "\n"
<< "edition: " << +secIS.edition << "\n"
<< "\n### Section PDS ###\n"
<< "size: " << +secPDS.size << "\n"
<< "tableVersion: " << +secPDS.tableVersion << "\n"
<< "indentificatorOfCenter: " << +secPDS.indentificatorOfCenter << "\n"
<< "numProcessID: " << +secPDS.numProcessID << "\n"
<< "gridIndentification: " << +secPDS.gridIndentification << "\n"
<< "flagForGDSorBMS: " << +secPDS.flagForGDSorBMS << "\n"
<< "indParamAndUnit: " << +secPDS.indParamAndUnit << "\n"
<< "indTypeOfLevelOrLayer: " << +secPDS.indTypeOfLevelOrLayer << "\n"
<< "levelOrLayer: " << +secPDS.levelOrLayer << "\n"
<< "year: " << +secPDS.year << "\n"
<< "month: " << +secPDS.month << "\n"
<< "day: " << +secPDS.day << "\n"
<< "hour: " << +secPDS.hour << "\n"
<< "minute: " << +secPDS.minute << "\n"
<< "forecastTimeUnit: " << +secPDS.forecastTimeUnit << "\n"
<< "p1: " << +secPDS.p1 << "\n"
<< "p2: " << +secPDS.p2 << "\n"
<< "indTimeRange: " << +secPDS.indTimeRange << "\n"
<< "averageOrAccumulate: " << +secPDS.averageOrAccumulate << "\n"
<< "missing: " << +secPDS.missing << "\n"
<< "century: " << +secPDS.century << "\n"
<< "subcenterId: " << +secPDS.subcenterId << "\n"
<< "decimalScale: " << +secPDS.decimalScale << "\n";
}
static GribData loadData(const ByteVec& rawdata) {
GribData result;
constexpr char MAGIC_START[4] = { 'G', 'R', 'I', 'B' };
constexpr char MAGIC_END[4] = { '7', '7', '7', '7' };
auto start = std::search(rawdata.cbegin(),
rawdata.cend(),
std::begin(MAGIC_START),
std::end(MAGIC_START));
auto end = std::search(rawdata.cbegin(),
rawdata.cend(),
std::begin(MAGIC_END),
std::end(MAGIC_END));
ByteVec data(start, end + sizeof(MAGIC_END));
result.secIS = section::IS::read(data.cbegin());
result.secPDS = section::PDS::read(data.cbegin() + 8);
auto size = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 4);
auto sec1 = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 8);
auto sec2 = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 8 + sec1);
auto sec3 = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 8 + sec1 + sec2);
std::cout
<< "size: " << size << "\n"
<< "sec0: " << 8 << "\n"
<< "sec1: " << sec1 << "\n"
<< "sec2: " << sec2 << "\n"
<< "sec3: " << sec3 << "\n"
<< "end flag: " << sizeof(MAGIC_END) << "\n"
<< "sum: " << 8 + sec1 + sec2 + sec3 + sizeof(MAGIC_END) << "\n\n";
return result;
}
static GribData loadDataFromFile(const std::string& path) {
std::ifstream file(path, std::ios::binary);
ByteVec data;
std::copy(std::istreambuf_iterator<char>(file),
{},
std::back_inserter(data));
return loadData(data);
}
};
int main() {
auto grib = GribData::loadDataFromFile("message_2_G1.grib");
grib.print();
}
分配值的标识符
assign
请注意,带有for (i in numbers_for_variable_name) {
assign(paste0("number-", i), "p1")
}
的标识符不是标准的,但可以使用-
。因此,如果我们想获取该值,请使用反引号
_
但是,建议不要在全局环境中具有多个对象。