簡體   English   中英

如何為R中的列中的新單詞分配數值

[英]How to assign numeric value to new word within column in R

我正在R中為像這樣的水果列表分配值。

#input csv of fruits

fruits
 apple
 pineapple
 orange
 pear

fruits <- data.frame(fruits = c("apple", "pineapple", "orange", "pear"))

mapping <- c("apple" = 1, "orange"= 10, "pear" = 5,"pineapple" = 11)

fruits$value <- mapping[fruits[,1]]

#output

fruits     value
 apple     1
 pineapple 11
 orange    10
 pear      5

當水果列表更新時,例如通過添加芒果和獼猴桃,我希望將水果中任何可能的意外單詞/文本分配為數值1,而不必將該特定單詞添加到映射中。

使用新的結果運行代碼將輸出此結果。

fruits     value
 apple     1
 pineapple NA
 orange    11
 pear      NA
 mango     5
 kiwi      10

fruits2 = data.frame(fruits = 
  c("apple", "pineapple", "orange", "pear", "mango", "kiwi"))

我希望北美不包括芒果和獼猴桃,但它們不可以。

如何將值1分配給原始映射中未包含的新水果?

謝謝

從您的映射向量創建映射數據幀,然后使用merge

mapdf = data.frame(fruits = names(mapping), value = mapping, row.names = NULL)

yourdf = merge(df,mapdf,by = 'fruits',all.x = T)
yourdf
     fruits value
1     apple     1
2      kiwi    NA
3     mango    NA
4    orange    10
5      pear     5
6 pineapple    11

關於將NA填入1

yourdf$value[is.na(yourdf$value)] = 1

首先,如果fruits是一個因素,將其轉化為個性

df$fruits <- as.character(df$fruits)

那你做

df$value <- mapping[df$fruits]

df
#     fruits value
#1     apple     1
#2 pineapple    11
#3    orange    10
#4      pear     5
#5     mango    NA
#6      kiwi    NA

這將為不在mappingfruits給出NA 現在將NA設為1。

df$value[is.na(df$value)] <- 1

df
#     fruits value
#1     apple     1
#2 pineapple    11
#3    orange    10
#4      pear     5
#5     mango     1
#6      kiwi     1

如何將值1分配給原始映射中未包含的新水果?

如果您只查找一列,則可以選擇match

mymapping = c(mapping, default = 1)
m = match(fruits2$fruits, names(mymapping), nomatch=length(mymapping))

fruits2$value = mymapping[m]

     fruits value
1     apple     1
2 pineapple    11
3    orange    10
4      pear     5
5     mango     1
6      kiwi     1

這是一個整潔的解決方案:

library(tidyverse)
fruits2 %>%
  left_join(enframe(mapping),by=c(fruits="name")) %>%
  replace_na(replace = list(value=1))
#      fruits value
# 1     apple     1
# 2 pineapple    11
# 3    orange    10
# 4      pear     5
# 5     mango     1
# 6      kiwi     1
# Warning message:
# Column `fruits`/`name` joining factor and character vector, coercing into character vector

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM