我正在尝试根据来自另一个数据库df2
的信息在数据库df1
中添加额外的列。我在sequence_annotation
中有一个名为df2
的列,如果该列具有数值,那么我希望它将信息复制到一个名为PoleX
和AnX
的字符中。这两个数据库的常量列/标识符称为CTsite
,因此这就是我要匹配来自PoleX
或AnX
的哪些值的方式。
df2看起来像这样:
CTsite sequence_annotation
OCCAJ01 Lt
OCCAJ01 20
OCCAJ04 Mt
OCCAJ04 40
df1看起来像这样:
CTsite AnX Pole X
OCCAJ01 720 2592
OCCAJ04 640 3264
错误的输出是:
CTsite sequence_annotation xres
OCCAJ01 Lt 720
OCCAJ01 20 2592
OCCAJ04 Mt 640
OCCAJ04 40 3264
我试图根据先前的问题和评论创建代码,但是它不起作用。如果有人可以帮助,将不胜感激。这是代码:
if (mode(df2$sequence_annotation)=="numeric") {
df2$xres <- df1$PoleX[match(df1$CTsite,df2$CTsite)]
} else {
df2$xres <- df1$AnX[match(df1$CTsite, df2$CTsite)]
}
答案 0 :(得分:0)
tidyverse的另一种解决方案。我怀疑您对数字的测试不足:该列的所有值必须共享相同的模式,可能是以下字符:
declare @n nchar(1), @str nchar(20)
set @n = nchar(8237)
select 'x'+@n+'x'
set @str = N'430'+@n+N'22' -- after "3" there is also an unicode 8237 char
select charindex( @n ,@str)
select patindex( '%%' ,@str) -- between %% there is an unicode 8237 char
答案 1 :(得分:0)
dplyr软件包可以提供帮助。但是由于我无法重现这一点,因此我无法提供太多帮助。这是备忘单
https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf