You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中移除字符向量部分内容及生成带.f后缀向量的方法

Got it, let's tackle these two R questions step by step!

1. Removing the .f suffix from list names

When you pull financial data with getFinancials(), the resulting list gets names like GOOG.f—here's how you can strip that .f off the end:

Base R approach

Assuming your financial data list is stored in an object called fin_data:

# Get the current list names, then remove the .f suffix from the end
names(fin_data) <- sub("\\.f$", "", names(fin_data))

The regex \\.f$ targets exactly .f at the end of each name (the \\. escapes the dot since it's a special character in regex, and $ anchors it to the end so we don't accidentally remove dots elsewhere).

Using stringr (tidyverse)

If you prefer the tidyverse syntax, stringr::str_remove() makes this even cleaner:

library(stringr)
names(fin_data) <- str_remove(names(fin_data), "\\.f$")
2. Creating a new character vector with .f suffixes

To make a symbols.f vector that adds .f to each original symbol (great for avoiding conflicts with getSymbols()), you can use simple string concatenation:

Base R approach

Start with your original symbols vector, then use paste0() to append .f without extra spaces:

symbols <- c("HOC", "GOOG", "MSFT") # Example original symbols
symbols.f <- paste0(symbols, ".f")
# Result: c("HOC.f", "GOOG.f", "MSFT.f")

Using stringr (tidyverse)

Alternatively, stringr::str_c() works the same way:

library(stringr)
symbols.f <- str_c(symbols, ".f")

This vector is ready to use wherever you need the suffixed symbol names, keeping things separate from your original symbols vector to prevent clashes with getSymbols().

内容的提问来源于stack exchange,提问作者user113156

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:15:18