-1

New to R here - Using Rstudio on a fedora25. I'm not sure if this is some syntax, data type or something else that is the issue here, but R is giving me the wrong output for adding/subtracting two columns in its data frame. Please have a look below and let me know what the issue is. Thanks!

Result Dataframe:

A   B       C   D   E   F   G
1   true    1   0   0   0   1
2   false   1   438 32  460 2
3   false   1   100 0   100 1
4   false   1   800 136 936 5
5   true    1   250 0   250 1

Code:

current['I'] <- as.numeric(unlist(current['D'])) + as.numeric(unlist(current['E']))
current['G'] <- as.numeric(unlist(current['F'])) - current['I']

current$C <- ifelse(current$G==0, "false", "true")

write.csv(current,"current.csv")

The df was read from salesforce. Thats why using unlist & as.numeric.

lmo
  • 36,904
  • 9
  • 50
  • 61
Dr Confuse
  • 555
  • 1
  • 5
  • 23
  • 1
    Could you paste the output of `dput(head(current))`? – Aurèle May 19 '17 at 16:23
  • Can you show us the "wrong" output? What do the `C`, `I`, and `G` columns look like? I will bet this is a case of you converting factors to numeric, but there's no way we can tell without more information. – Spacedman May 19 '17 at 16:42

2 Answers2

1

I am not familiar with the way you are selecting your columns, normally you select columns in data frames using the column number or name in the following way (for column D): current[,4] or current$D

Nevertheless, check if the columns C to G are factors. If they are, the problem may be in the conversion of factor to numeric. Try

as.numeric(levels(current$D))[current$D]

Check this answer: How to convert a factor to an integer\numeric without a loss of information?

Community
  • 1
  • 1
sbg
  • 1,732
  • 7
  • 27
  • 44
0

If this is a dataframe, you may be referencing the column names incorrectly.

Use

current$I <- as.numeric(unlist(current$D)) + as.numeric(unlist(current$E))
current$G <- as.numeric(unlist(current$F)) - current$I
current$C <- ifelse(current$G == 0, "false", "true")
write.csv(current, "current.csv")
akash87
  • 3,718
  • 3
  • 13
  • 29