gpt4 book ai didi

r - 在列表中用空格替换句点时出错

转载 作者:行者123 更新时间:2023-12-02 02:03:20 25 4
gpt4 key购买 nike

我有一个名称中带有句点的指标列表,我想用空格替换这些句点。我知道 gsub() 函数可以替换标点符号。但每次我尝试用空格替换点时,列表都会返回 null

list_AM = list(list(geo = "EU", sales="West.Europe.Sales",
indicator = list("SA","NSA","composites_industry_value","DUCS","WUCS","T30","Rovings",
"Mats","WE.Construction.Gross.output..sales...Real.USD","WE.Construction.Production.index","WE.Glass.Gross.operating.surplus..profits...Nominal.USD",
"WE.Glass.Gross.output..sales...Nominal.USD","WE.Glass.Investment..Nominal.USD","WE.Glass.Production.index","WE.Glass.Value.added.output..As.a.percent.of.GDP",
"WE.Glass.Value.added.output..As.a.percent.of.manufacturing","WE.Glass.Value.added.output..As.a.percent.of.world.total","WE.Industrial.Production.Gross.operating.surplus..profits...Nominal.USD",
"WE.Industrial.Production.Gross.output..sales...Nominal.USD","WE.Glass.Investment..Nominal.USD","WE.Glass.Production.index","WE.Glass.Value.added.output..As.a.percent.of.GDP","WE.Glass.Value.added.output..As.a.percent.of.manufacturing",
"WE.Glass.Value.added.output..As.a.percent.of.world.total","WE.Industrial.Production.Gross.operating.surplus..profits...Nominal.USD","WE.Industrial.Production.Gross.output..sales...Nominal.USD","WE.Industrial.Production.Production.index",
"WE.Industrial.Production.Value.added.output..As.a.percent.of.GDP","WE.Industrial.Production.Value.added.output..As.a.percent.of.world.total","WE.Manufacturing.Gross.operating.surplus..profits...Nominal.USD","WE.Manufacturing.Gross.output..sales...Nominal.USD",
"WE.Manufacturing.Investment..Nominal.USD","WE.Manufacturing.Production.index","WE.Manufacturing.Value.added.output..As.a.percent.of.GDP","WE.Manufacturing.Value.added.output..As.a.percent.of.world.total","WE.Current.account.of.balance.of.payments.in.US...share.of.GDP",
"WE.Employment..total.1","WE.External.debt..total..US.","WE.Foreign.direct.investment..US.","WE.GDP.per.capita..nominal..US.","WE.GDP..nominal..US.","WE.Government.balance..share.of.GDP","WE.Population..total","WE.Reserves..foreign.exchange..US.",
"WE.Reserves..months.of.import.cover","WE.Stockbuilding..real..share.of.GDP","WE.Visible.trade.balance..share.of.GDP","WE.Consumer.price.index","WE.Gross.government.debt..as.a...of.GDP.","WE.Industrial.production.index","WE.Interest.rate..short.term",
"WE.Interest.rate..Yield.on.10.year.Government.Debt.Securities....per.annum.","WE.Services.balance..as...of.GDP","WE.Share.price.index","WE.Unemployment.rate","WE.Capacity.utilisation","WE.Consumption..government..PPP.exchange.rate..nominal..US.","WE.Consumption..government..nominal..US.",
"WE.Consumption..government..nominal..share.of.GDP.1","WE.Consumption..private..PPP.exchange.rate..nominal..US.","WE.Exports..goods...services..constant.prices.and.exchange.rate..US.....of.World","WE.GDP..industry..real","WE.GVA.Agriculture.share.of.GVA","WE.GVA.Industry.share.of.GVA",
"WE.GVA.Manufacturing.of.GVA","WE.GVA.Services..share.of.GVA","WE.Gross.value.added.in.construction..real","WE.Gross.value.added.in.services..real","WE.Imports..goods...services..constant.prices.and.exchange.rate..US.....of.World","WE.Imports..goods..PPP.exchange.rate..nominal..US.",
"WE.Industrial.production.index.1","WE.Investment..government..nominal","WE.Investment..machinery...equipment..nominal","WE.Investment..private..non.residential.structures..nominal","WE.Investment..total.fixed.investment..nominal..US.",
"WE.Investment..total.fixed..nominal..share.of.GDP","WE.Net.investment..nominal..US.","WE.Output.gap","WE.Productivity..trend","WE.Stockbuilding..nominal..US.",
"WE.Stockbuilding..nominal..share.of.GDP","WE.Stockbuilding..real..annual.contribution.to.growth","WE.Trend.productivity.target","WE.World.trade.index","WE.House.price.index","WE.Housing.starts","WE.Interest.rate.on.building.society.mortgages","WE.Market.value.of.housing.stock..LCU",
"WE.Residential.property.transactions","WE.Stock.of.owner.occupied.houses","WE.Consumers..expenditure..durables..nominal","WE.Financial.liabilities..household.sector..as.a...of.disposable.income","WE.Liabilities..debt.other.than.loans..households","WE.Personal.consumer.credit",
"WE.Retail.sales..value.index","WE.Retail.sales..volume.index","WE.Savings..personal.sector.ratio")))

例如,我希望返回列表而不是“WE.Residential.property.transactions”“WE住宅属性(property)交易”

最佳答案

根据结构,它是一个递归列表,因此,函数以递归方式循环嵌套列表,即rapplyrrapply 可以使用并应用 gsub 来匹配 . 并替换为空格 (' ')。

请注意,. 是一个元字符,可以匹配正则表达式模式(默认情况)中的任何字符,因此我们可以使用 fixed = TRUE 进行字面匹配(应该更快) ) 或转义 (\\.) 或将其放在方括号内 ([.])

library(rrapply)
list_AM2 <- rrapply(list_AM, f = function(x) gsub(".", " ", x, fixed = TRUE))

-输出

> list_AM2
[[1]]
[[1]]$geo
[1] "EU"

[[1]]$sales
[1] "West Europe Sales"

[[1]]$indicator
[[1]]$indicator[[1]]
[1] "SA"

[[1]]$indicator[[2]]
[1] "NSA"

[[1]]$indicator[[3]]
[1] "composites_industry_value"

[[1]]$indicator[[4]]
[1] "DUCS"

[[1]]$indicator[[5]]
[1] "WUCS"

[[1]]$indicator[[6]]
[1] "T30"

[[1]]$indicator[[7]]
[1] "Rovings"

[[1]]$indicator[[8]]
[1] "Mats"

[[1]]$indicator[[9]]
[1] "WE Construction Gross output sales Real USD"

[[1]]$indicator[[10]]
[1] "WE Construction Production index"

[[1]]$indicator[[11]]
[1] "WE Glass Gross operating surplus profits Nominal USD"

[[1]]$indicator[[12]]
[1] "WE Glass Gross output sales Nominal USD"

[[1]]$indicator[[13]]
[1] "WE Glass Investment Nominal USD"

[[1]]$indicator[[14]]
[1] "WE Glass Production index"

[[1]]$indicator[[15]]
[1] "WE Glass Value added output As a percent of GDP"

[[1]]$indicator[[16]]
[1] "WE Glass Value added output As a percent of manufacturing"

[[1]]$indicator[[17]]
[1] "WE Glass Value added output As a percent of world total"

[[1]]$indicator[[18]]
[1] "WE Industrial Production Gross operating surplus profits Nominal USD"

[[1]]$indicator[[19]]
[1] "WE Industrial Production Gross output sales Nominal USD"

[[1]]$indicator[[20]]
[1] "WE Glass Investment Nominal USD"

[[1]]$indicator[[21]]
[1] "WE Glass Production index"

[[1]]$indicator[[22]]
[1] "WE Glass Value added output As a percent of GDP"

[[1]]$indicator[[23]]
[1] "WE Glass Value added output As a percent of manufacturing"

[[1]]$indicator[[24]]
[1] "WE Glass Value added output As a percent of world total"

[[1]]$indicator[[25]]
[1] "WE Industrial Production Gross operating surplus profits Nominal USD"

[[1]]$indicator[[26]]
[1] "WE Industrial Production Gross output sales Nominal USD"

[[1]]$indicator[[27]]
[1] "WE Industrial Production Production index"

[[1]]$indicator[[28]]
[1] "WE Industrial Production Value added output As a percent of GDP"

[[1]]$indicator[[29]]
[1] "WE Industrial Production Value added output As a percent of world total"

[[1]]$indicator[[30]]
[1] "WE Manufacturing Gross operating surplus profits Nominal USD"

[[1]]$indicator[[31]]
[1] "WE Manufacturing Gross output sales Nominal USD"

[[1]]$indicator[[32]]
[1] "WE Manufacturing Investment Nominal USD"

[[1]]$indicator[[33]]
[1] "WE Manufacturing Production index"

[[1]]$indicator[[34]]
[1] "WE Manufacturing Value added output As a percent of GDP"

[[1]]$indicator[[35]]
[1] "WE Manufacturing Value added output As a percent of world total"

[[1]]$indicator[[36]]
[1] "WE Current account of balance of payments in US share of GDP"

[[1]]$indicator[[37]]
[1] "WE Employment total 1"

[[1]]$indicator[[38]]
[1] "WE External debt total US "

[[1]]$indicator[[39]]
[1] "WE Foreign direct investment US "

[[1]]$indicator[[40]]
[1] "WE GDP per capita nominal US "

[[1]]$indicator[[41]]
[1] "WE GDP nominal US "

[[1]]$indicator[[42]]
[1] "WE Government balance share of GDP"

[[1]]$indicator[[43]]
[1] "WE Population total"

[[1]]$indicator[[44]]
[1] "WE Reserves foreign exchange US "

[[1]]$indicator[[45]]
[1] "WE Reserves months of import cover"

[[1]]$indicator[[46]]
[1] "WE Stockbuilding real share of GDP"

[[1]]$indicator[[47]]
[1] "WE Visible trade balance share of GDP"

[[1]]$indicator[[48]]
[1] "WE Consumer price index"

[[1]]$indicator[[49]]
[1] "WE Gross government debt as a of GDP "

[[1]]$indicator[[50]]
[1] "WE Industrial production index"

[[1]]$indicator[[51]]
[1] "WE Interest rate short term"

[[1]]$indicator[[52]]
[1] "WE Interest rate Yield on 10 year Government Debt Securities per annum "

[[1]]$indicator[[53]]
[1] "WE Services balance as of GDP"

[[1]]$indicator[[54]]
[1] "WE Share price index"

[[1]]$indicator[[55]]
[1] "WE Unemployment rate"

[[1]]$indicator[[56]]
[1] "WE Capacity utilisation"

[[1]]$indicator[[57]]
[1] "WE Consumption government PPP exchange rate nominal US "

[[1]]$indicator[[58]]
[1] "WE Consumption government nominal US "

[[1]]$indicator[[59]]
[1] "WE Consumption government nominal share of GDP 1"

[[1]]$indicator[[60]]
[1] "WE Consumption private PPP exchange rate nominal US "

[[1]]$indicator[[61]]
[1] "WE Exports goods services constant prices and exchange rate US of World"

[[1]]$indicator[[62]]
[1] "WE GDP industry real"

[[1]]$indicator[[63]]
[1] "WE GVA Agriculture share of GVA"

[[1]]$indicator[[64]]
[1] "WE GVA Industry share of GVA"

[[1]]$indicator[[65]]
[1] "WE GVA Manufacturing of GVA"

[[1]]$indicator[[66]]
[1] "WE GVA Services share of GVA"

[[1]]$indicator[[67]]
[1] "WE Gross value added in construction real"

[[1]]$indicator[[68]]
[1] "WE Gross value added in services real"

[[1]]$indicator[[69]]
[1] "WE Imports goods services constant prices and exchange rate US of World"

[[1]]$indicator[[70]]
[1] "WE Imports goods PPP exchange rate nominal US "

[[1]]$indicator[[71]]
[1] "WE Industrial production index 1"

[[1]]$indicator[[72]]
[1] "WE Investment government nominal"

[[1]]$indicator[[73]]
[1] "WE Investment machinery equipment nominal"

[[1]]$indicator[[74]]
[1] "WE Investment private non residential structures nominal"

[[1]]$indicator[[75]]
[1] "WE Investment total fixed investment nominal US "

[[1]]$indicator[[76]]
[1] "WE Investment total fixed nominal share of GDP"

[[1]]$indicator[[77]]
[1] "WE Net investment nominal US "

[[1]]$indicator[[78]]
[1] "WE Output gap"

[[1]]$indicator[[79]]
[1] "WE Productivity trend"

[[1]]$indicator[[80]]
[1] "WE Stockbuilding nominal US "

[[1]]$indicator[[81]]
[1] "WE Stockbuilding nominal share of GDP"

[[1]]$indicator[[82]]
[1] "WE Stockbuilding real annual contribution to growth"

[[1]]$indicator[[83]]
[1] "WE Trend productivity target"

[[1]]$indicator[[84]]
[1] "WE World trade index"

[[1]]$indicator[[85]]
[1] "WE House price index"

[[1]]$indicator[[86]]
[1] "WE Housing starts"

[[1]]$indicator[[87]]
[1] "WE Interest rate on building society mortgages"

[[1]]$indicator[[88]]
[1] "WE Market value of housing stock LCU"

[[1]]$indicator[[89]]
[1] "WE Residential property transactions"

[[1]]$indicator[[90]]
[1] "WE Stock of owner occupied houses"

[[1]]$indicator[[91]]
[1] "WE Consumers expenditure durables nominal"

[[1]]$indicator[[92]]
[1] "WE Financial liabilities household sector as a of disposable income"

[[1]]$indicator[[93]]
[1] "WE Liabilities debt other than loans households"

[[1]]$indicator[[94]]
[1] "WE Personal consumer credit"

[[1]]$indicator[[95]]
[1] "WE Retail sales value index"

[[1]]$indicator[[96]]
[1] "WE Retail sales volume index"

[[1]]$indicator[[97]]
[1] "WE Savings personal sector ratio"

如果有多个.,可以使用\\.+,即一个或多个,并替换为' '

list_AM2 <- rrapply(list_AM, f = function(x) gsub("\\.+", " ", x))

关于r - 在列表中用空格替换句点时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68732164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com