使用R中的键的变量标签?

使用R中的键的变量标签?,r,R,假设我在R中导入了一个csv,它看起来是这样的 1 4 3 5 9 6 2 6 我有另一个csv,类似于 1 apple 2 banana 3 toast 4 beer 5 chips 6 cheese 9 wine 有没有一种方法可以使用第二种方法来标记第一种方法中的值?我希望这是有道理的 以下是一种可能的方法: # recreate your input data vals <- read.table( text= "1 4 3 5 9 6 2 6") map <- re

假设我在R中导入了一个csv,它看起来是这样的

1 4 3 5
9 6 2 6 
我有另一个csv,类似于

1 apple
2 banana
3 toast
4 beer
5 chips
6 cheese
9 wine

有没有一种方法可以使用第二种方法来标记第一种方法中的值?我希望这是有道理的

以下是一种可能的方法:

# recreate your input data
vals <- read.table(
text=
"1 4 3 5
9 6 2 6")

map <- read.table(
text=
"1 apple
2 banana
3 toast
4 beer
5 chips
6 cheese
9 wine",stringsAsFactors=F)
names(map) <- c('K','V')


# - turn vals into a vector (using unlist function)
# - get the row indexes of the unlisted keys corresponding 
#   to map keys (using match function)
# - using the indexes select the key names in map (using vector subsetting [])
# - turn the character vector into 2d matrix with original dimension of vals
# - coerce to data.frame
mapped <- as.data.frame(matrix(map$V[match(unlist(vals),map$K)],nrow=nrow(vals)))

# > mapped
# V1     V2     V3     V4
# 1 apple   beer  toast  chips
# 2  wine cheese banana cheese
#重新创建输入数据

VAL以下是一种可能的方法:

# recreate your input data
vals <- read.table(
text=
"1 4 3 5
9 6 2 6")

map <- read.table(
text=
"1 apple
2 banana
3 toast
4 beer
5 chips
6 cheese
9 wine",stringsAsFactors=F)
names(map) <- c('K','V')


# - turn vals into a vector (using unlist function)
# - get the row indexes of the unlisted keys corresponding 
#   to map keys (using match function)
# - using the indexes select the key names in map (using vector subsetting [])
# - turn the character vector into 2d matrix with original dimension of vals
# - coerce to data.frame
mapped <- as.data.frame(matrix(map$V[match(unlist(vals),map$K)],nrow=nrow(vals)))

# > mapped
# V1     V2     V3     V4
# 1 apple   beer  toast  chips
# 2  wine cheese banana cheese
#重新创建输入数据

VAL以下是一种可能的方法:

# recreate your input data
vals <- read.table(
text=
"1 4 3 5
9 6 2 6")

map <- read.table(
text=
"1 apple
2 banana
3 toast
4 beer
5 chips
6 cheese
9 wine",stringsAsFactors=F)
names(map) <- c('K','V')


# - turn vals into a vector (using unlist function)
# - get the row indexes of the unlisted keys corresponding 
#   to map keys (using match function)
# - using the indexes select the key names in map (using vector subsetting [])
# - turn the character vector into 2d matrix with original dimension of vals
# - coerce to data.frame
mapped <- as.data.frame(matrix(map$V[match(unlist(vals),map$K)],nrow=nrow(vals)))

# > mapped
# V1     V2     V3     V4
# 1 apple   beer  toast  chips
# 2  wine cheese banana cheese
#重新创建输入数据

VAL以下是一种可能的方法:

# recreate your input data
vals <- read.table(
text=
"1 4 3 5
9 6 2 6")

map <- read.table(
text=
"1 apple
2 banana
3 toast
4 beer
5 chips
6 cheese
9 wine",stringsAsFactors=F)
names(map) <- c('K','V')


# - turn vals into a vector (using unlist function)
# - get the row indexes of the unlisted keys corresponding 
#   to map keys (using match function)
# - using the indexes select the key names in map (using vector subsetting [])
# - turn the character vector into 2d matrix with original dimension of vals
# - coerce to data.frame
mapped <- as.data.frame(matrix(map$V[match(unlist(vals),map$K)],nrow=nrow(vals)))

# > mapped
# V1     V2     V3     V4
# 1 apple   beer  toast  chips
# 2  wine cheese banana cheese
#重新创建输入数据

VAL所涉及对象的R类是什么。您是否将第一个CSV中的数据视为data.frame?字符矩阵?所需输出的类别是什么?与其粘贴随机文本,不如使用适当的R对象创建一个文本,这样我们就可以看到结构。请查看该链接上的答案,了解如何执行此操作的提示。您可以执行类似于
x2$col1[match(name(x1)、rownames(x2))]
所涉及对象的R类是什么。您是否将第一个CSV中的数据视为data.frame?字符矩阵?所需输出的类别是什么?与其粘贴随机文本,不如使用适当的R对象创建一个文本,这样我们就可以看到结构。请查看该链接上的答案,了解如何执行此操作的提示。您可以执行类似于
x2$col1[match(name(x1)、rownames(x2))]
所涉及对象的R类是什么。您是否将第一个CSV中的数据视为data.frame?字符矩阵?所需输出的类别是什么?与其粘贴随机文本,不如使用适当的R对象创建一个文本,这样我们就可以看到结构。请查看该链接上的答案,了解如何执行此操作的提示。您可以执行类似于
x2$col1[match(name(x1)、rownames(x2))]
所涉及对象的R类是什么。您是否将第一个CSV中的数据视为data.frame?字符矩阵?所需输出的类别是什么?与其粘贴随机文本,不如使用适当的R对象创建一个文本,这样我们就可以看到结构。查看该链接上的答案,了解如何执行此操作的提示。您可以执行类似于
x2$col1[match(name(x1),rownames(x2))]
Edit:merge返回无序的行,因此在这种情况下几乎不可用。此解决方案使用@RichardScrivenEdit:merge建议的匹配函数返回无序行,因此在这种情况下几乎不可用。此解决方案使用@RichardScrivenEdit:merge建议的匹配函数返回无序行,因此在这种情况下几乎不可用。此解决方案使用@RichardScrivenEdit:merge建议的匹配函数返回无序行,因此在这种情况下几乎不可用。此解决方案使用@RichardScriven建议的匹配函数