映射列值 [英] Mapping column values

查看:104
本文介绍了映射列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用某些映射函数来转换给定列的值。示例:

I would like to transform the values of a given column using some mapping function. Example:

df <- data.frame(A = 1:5, B = sample(1:20, 10))
df
   A  B
1  1 17
2  2  5
3  3  3
4  4 11
5  5 19
6  1 16
7  2  4
8  3  7
9  4  6
10 5  9

我的目标是将A列的所有元素映射如下:

My goal is to map all elements of column A as following:

1 -> "tt"
2 -> "ff"
3 -> "ss"
4 -> "fs"
5 -> "sf"

我写了以下内容:

mappingList <- c("tt", "ff", "ss", "fs", "sf")
df$A <- unlist(lapply(df$A, function(x){replace(x, x>0, mappingList[x])}))
df
  A  B
1  tt 17
2  ff  5
3  ss  3
4  fs 11
5  sf 19
6  tt 16
7  ff  4
8  ss  7
9  fs  6
10 sf  9

上面的代码很好用。

现在,让我们假设另一个数据框,其中A列不是由整数1,2,3,4,5组成,而是由其他任何通用项组成,例如:

Now let's assume another dataframe where column A is not made of integers 1,2,3,4,5 but rather any other 'generic' items, say:

df <- data.frame(A = paste("str",1:5,sep=""), B = sample(1:20, 10))

df <- data.frame(A = seq(5, 25, by=5), B = sample(1:20, 10))

问题:如何编写映射?

Question: How would you write the mapping ?

推荐答案

您看过因素吗?

df$A_2 <- factor(df$A, levels = 1:5, labels = c("tt", "ff", "ss", "fs", "sf"))
df
#    A  B A_2
# 1  1 17  tt
# 2  2  5  ff
# 3  3  3  ss
# 4  4 11  fs
# 5  5 19  sf
# 6  1 16  tt
# 7  2  4  ff
# 8  3  7  ss
# 9  4  6  fs
# 10 5  9  sf

基本上,您的 levels 参数应具有匹配的原始值,而您的 labels 参数应具有替换值。

Basically, your levels argument should have the original values to match, and your labels argument should have the replacement values.

您还可以创建带有命名矢量的查找表。

You could also create a look-up table with a named vector.

示例:

df <- data.frame(A = paste("str",1:5,sep=""), B = sample(1:20, 10))

NamedVec <- setNames(paste("str",1:5,sep=""), c("tt", "ff", "ss", "fs", "sf"))
NamedVec
#     tt     ff     ss     fs     sf
# "str1" "str2" "str3" "str4" "str5" 
NamedVec[df$A]
#     tt     ff     ss     fs     sf     tt     ff     ss     fs     sf 
# "str1" "str2" "str3" "str4" "str5" "str1" "str2" "str3" "str4" "str5" 
names(NamedVec[df$A])
#  [1] "tt" "ff" "ss" "fs" "sf" "tt" "ff" "ss" "fs" "sf"

这篇关于映射列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆