2

I have a data frame as below and would like to generate another data frame by replacing the characters in the dataframe into numeric according condition.

condition:
GD  5
O   4.5
AV  3
B   2.5

dataframe:
Name    Peter   Paul    Mary    Chan    Jose
Nokia   B       O       O       GD      GD
iPhone  AV      O       O       O       O
Moto    AV      AV      O       O       AV
HTC     B       O       AV      AV      AV

dput:
structure(list(Name = c("Nokia", "iPhone", "Moto", "HTC"), Peter = 
c("B", "AV", "AV", "B"), Paul = c("O", "O", "AV", "O"), Mary = 
c("O", "O", "O", "AV"), Chan = c("GD", "O", "O", "AV"), Jose = 
c("GD", "O", "AV", "AV")), .Names = 
c("Name", "Peter", "Paul", "Mary", "Chan", "Jose"), class = "data.frame", row.names = c(NA, -4L))

desired output:
Name    Peter   Paul    Mary    Chan    Jose
Nokia   2.5     4.5     4.5     5       5
iPhone  3       4.5     4.5     4.5     4.5
Moto    3       3       4.5     4.5     3
HTC     2.5     4.5     3       3       3

Thanks for your help!!!

3 Answers 3

7

I'm not sure what is the structure of condition, but assuming it's a named vector as such

condition <- setNames(c(5, 4.5, 3, 2.5), c("GD", "O", "AV", "B"))

I would simply, unlist/match your data set with it and put it back

df[-1] <- condition[match(unlist(df[-1]), names(condition))]
df
#     Name Peter Paul Mary Chan Jose
# 1  Nokia   2.5  4.5  4.5  5.0  5.0
# 2 iPhone   3.0  4.5  4.5  4.5  4.5
# 3   Moto   3.0  3.0  4.5  4.5  3.0
# 4    HTC   2.5  4.5  3.0  3.0  3.0
Sign up to request clarification or add additional context in comments.

Comments

3

One option is using match by looping through the columns

df1[-1] <- lapply(df1[-1], function(x) keyval$v2[match(x, keyval$v1)])
df1
#    Name Peter Paul Mary Chan Jose
#1  Nokia   2.5  4.5  4.5  5.0  5.0
#2 iPhone   3.0  4.5  4.5  4.5  4.5
#3   Moto   3.0  3.0  4.5  4.5  3.0
#4    HTC   2.5  4.5  3.0  3.0  3.0

where 'keyval' is a 'data.frame' with 'v1' and 'v2' as the 'key' and 'value' columns.

Comments

1

This also works (where cdf is the lookup table):

cbind.data.frame(Name=df[,1], matrix(cdf$value[match(as.matrix(df[-1]), cdf$condition)], 
                                     nrow=nrow(df), dimnames=list(NULL, names(df[-1]))))

with output

    Name Peter Paul Mary Chan Jose
1  Nokia   2.5  4.5  4.5  5.0  5.0
2 iPhone   3.0  4.5  4.5  4.5  4.5
3   Moto   3.0  3.0  4.5  4.5  3.0
4    HTC   2.5  4.5  3.0  3.0  3.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.