当数据框的行中有某个字符时,如何插入给定的描述

时间:2019-07-11 18:40:24

标签: r

我有从某些文件创建的以下表格:

table1.csv:

cod description
101 some description1
102 some description2
201 some description3
212 some description5
301 some description76
302 some description23
411 some description45
512 some description12

table2.txt:

cod title
0   title1
1   title2
2   title14
3   title17
4   title13
5   title19

将这些数据插入变量的代码:

table1 <- read.table("table1.csv",sep="\t",
                    header = TRUE,na.strings = '', colClasses = NA)

table2 <- read.table("table2.txt", sep = "\t",header = TRUE)

所以我想要做的,但我不知道怎么做,是将具有来自表2的代码1的描述插入到表1中,其中HEADER代码的第一个字符也为1,依此类推。 。在表1中的描述具有代码2的地方,表2中也插入了HEADER代码的第一个字符,在表2中。依此类推。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以将within中的matchcod table2substr进行1 st cod来自table1

table1 <- within(table1, {
  new <- table2$description[match(substr(table1$cod, 1, 1), table2$cod)]
})
table1
#   cod        description     new
# 1 101  some description1  title2
# 2 102  some description2  title2
# 3 201  some description3 title14
# 4 212  some description5 title14
# 5 301 some description76 title17
# 6 302 some description23 title17
# 7 411 some description45 title13
# 8 512 some description12 title19   

table2 <- within(table2, {
  new <- table1$description[match(table2$cod, substr(table1$cod, 1, 1))]
})
table2
#   cod description                new
# 1   0      title1               <NA>
# 2   1      title2  some description1
# 3   2     title14  some description3
# 4   3     title17 some description76
# 5   4     title13 some description45
# 6   5     title19 some description12

数据

table1 <- structure(list(cod = structure(1:8, .Label = c("101", "102", 
"201", "212", "301", "302", "411", "512", "cod"), class = "factor"), 
    description = structure(c(2L, 4L, 6L, 8L, 9L, 5L, 7L, 3L), .Label = c("description", 
    "some description1", "some description12", "some description2", 
    "some description23", "some description3", "some description45", 
    "some description5", "some description76"), class = "factor")), row.names = c(NA, 
-8L), class = "data.frame")

table2 <- structure(list(cod = structure(1:6, .Label = c("0", "1", "2", 
"3", "4", "5", "cod"), class = "factor"), description = structure(c(2L, 
7L, 4L, 5L, 3L, 6L), .Label = c("title", "title1", "title13", 
"title14", "title17", "title19", "title2"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame")