我有两个字符串:
mystring1 <- c("hello i am a cat. just kidding, i'm not a cat i'm a cat. dogs are the best animal. not cats!")
mystring2 <- c("hello i am a cat. just kidding, i'm not a cat i'm a cat. but i have a cat friend that is a cat.")
Run Code Online (Sandbox Code Playgroud)
我想将两个字符串中第三次出现的单词 cat 更改为 dog。
理想的情况下,string1与string2内容如下:
mystring1
[1] "hello i am a cat. just kidding, i'm not a cat i'm a dog. dogs are the best animal. …Run Code Online (Sandbox Code Playgroud) 我正在使用ComplexHeatmap在 R 中创建热图。我在这里重新创建了一个小热图。我无法从文档中弄清楚如何调整 x 轴上文本的字体大小。
a = matrix(1:9, nrow = 3, ncol = 3)
rownames(a) = c("alphabet","xylophone","tornado")
colnames(a) = c("c1","c2","c3")
my_heatmap = ComplexHeatmap::Heatmap(
matrix = t(a),
col = RColorBrewer::brewer.pal(9, "RdBu"))
Run Code Online (Sandbox Code Playgroud)
这段代码产生这样的结果:

我想调整c("alphabet","xylophone","tornado")文本的字体大小,使其小得多。我该怎么做呢?
我在 R 中有一个数据框。对于每一行,我想选择哪一列具有最高值,并粘贴该列的名称。当只有两列可供选择时,这很简单(请注意,如果两列的值都小于 0.1,我有一个不包括行的过滤步骤):
set.seed(6)
mat_simple <- matrix(rexp(200, rate=.1), ncol=2) %>%
as.data.frame()
head(mat_simple)
V1 V2
1 2.125366 6.7798683
2 1.832349 8.9610534
3 6.149668 15.7777370
4 3.532614 0.2355711
5 21.110703 1.2927119
6 2.871455 16.7370847
mat_simple <- mat_simple %>%
mutate(
class = case_when(
V1 < 0.1 & V2 < 0.1 ~ NA_character_,
V1 > V2 ~ "V1",
V2 > V1 ~ "V2"
)
)
head(mat_simple)
V1 V2 class
1 2.125366 6.7798683 V2
2 1.832349 8.9610534 V2
3 6.149668 15.7777370 V2
4 3.532614 …Run Code Online (Sandbox Code Playgroud) 我在 R 中有一个字符串,其中单词以随机数量的字符间隔\n:
mystring = c("hello\n\ni\n\n\n\n\nam\na\n\n\n\n\n\n\ndog")
Run Code Online (Sandbox Code Playgroud)
我想替换 n 个重复\n元素,以便单词之间只有一个空格字符。我目前可以按如下方式执行此操作,但我想要一个更简洁的解决方案:
mystring %>%
gsub("\n\n", "\n", .) %>%
gsub("\n\n", "\n", .) %>%
gsub("\n\n", "\n", .) %>%
gsub("\n", " ", .)
[1] "hello i am a dog"
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最佳方法是什么?
我在 R 中有一个字符串:
c("FLT1", "FLT1P1", "FLT1-FLT2", "SGY-FLT1, GPD")
Run Code Online (Sandbox Code Playgroud)
我想保留所有具有 FLT1 的匹配项,但在添加其他字母数字字符时则不保留。换句话说,我想保留除第二个条目之外的所有条目,因为它们都提到了 FLT1,但第二个条目提到了 FLT1P1。
当我使用 str_detect 时,它返回所有内容为 true:
str_detect(string, "FLT1")
[1] TRUE TRUE TRUE TRUE
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议仅退回提及 FLT1 的物品的最佳方法吗?
我有一个带有五列的制表符分隔文本文件 (animals.txt):
302947298 2340974238 0 0 cat
345098948 8345988989 0 0 dog
098982388 2098340923 0 0 fish
932840923 0923840988 0 0 parrot
Run Code Online (Sandbox Code Playgroud)
我有另一个文件,mess.txt.gz,它是使用 GNU zip(.gz 文件)压缩的。它基本上看起来像一串巨大的字母:
sdihfoiahdfosparrotdhiafoihsdfoijaslkdogoieufoiweuf
Run Code Online (Sandbox Code Playgroud)
基本上,对于制表符分隔的文本文件中的每一行,我想查看此 .gz 文件中是否存在任何动物名称。
理想情况下,它会返回如下内容:
302947298 2340974238 0 0 cat no
345098948 8345988989 0 0 dog yes
098982388 2098340923 0 0 fish no
932840923 0923840988 0 0 parrot yes
Run Code Online (Sandbox Code Playgroud)
目前我正在做以下事情:
gunzip -cd mess.txt.gz | grep cat
gunzip -cd mess.txt.gz | grep dog
Run Code Online (Sandbox Code Playgroud)
为了自动化,我尝试了以下方法:
cat animals.txt | awk '{print $5}' > animal_names.txt
cat animal_names.txt | …Run Code Online (Sandbox Code Playgroud) 我有一个数据框 df:
df <- data.frame(a = c("b2","d2","a1","c1"), b = c(12, 3, 54, 4))
> df
a b
1 b2 12
2 d2 3
3 a1 54
4 c1 4
Run Code Online (Sandbox Code Playgroud)
和一个外部向量,我希望其顺序a匹配:
vec <- c("a1","b2","c1","d2")
Run Code Online (Sandbox Code Playgroud)
通常我可以使用以下方法执行此操作match:
df <- df[match(vec, df$a),]
> df
a b
3 a1 54
1 b2 12
4 c1 4
2 d2 3
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否有办法在 dplyr 中做到这一点。我尝试过以下方法,但没有成功:
df <- df %>%
mutate(
a = match(vec, a)
)
> df
a b
1 3 12
2 1 …Run Code Online (Sandbox Code Playgroud) 给定一个整数数组,我可以找到具有最大乘积的一对相邻元素并使用 apply 返回该乘积吗?
可以使用apply函数完成吗?
预期输入:
x <- c(3, 6, -2, -5, 7, 3)
Run Code Online (Sandbox Code Playgroud)
预期输出:
adjacent_elements_product(x) = 21
Run Code Online (Sandbox Code Playgroud)
可能相关的类似帖子: 给定一个整数数组,找到具有最大乘积的相邻元素对并返回该乘积