Changing value of data frame based on another data frame - r

Again I need your help for a maybe easy question that is not clear for a starter R user.
I need to manipulate a dataframe to substitute NA values by "realistic" ones to feed another application.
The data frame contains values of -3.0 that was the flag for non valid values in the original data base. What I need is to replace all the -3.0 values by data coming from another data frame, or maybe to interpolate.
The first data frame would be
1.0 2.0 3.0 4.0
2.0 3.0 -3.0 -3.0
1.0 4.0 -3.0 6.0
1.0 5.0 4.0 5.0
the second one would be
1.0 1.0 1.0 1.0
2.0 2.0 9.0 9.0
2.0 2.0 9.0 2.0
1.0 1.0 1.0 1.0
and the expected result
1.0 2.0 3.0 4.0
2.0 3.0 9.0 9.0
1.0 4.0 9.0 6.0
1.0 5.0 4.0 5.0
I suppose this can be done with a for loop but I haven't found the way to do it.
Thanks in advance

It's actually quite simple to do this without a for loop: if your data frames are A and B, then the command would be
A[A == -3] = B[A == -3]
In other words: for all the indices of A that have value -3, assign the values of B at the corresponding indices.

Related

Functions R Programming

I have information about revenue in cumulative form for the whole year, I would like to get monthly revenue. Lets say first month revenue is 3.2M, and second month revenue is 2.2M, but my second entry is sum of first two months.
Revenue
3.2
5.4
7.6
9.2
I would like to extract revenue as below
ExRevenue
3.2
2.2
2.2
1.6
How can I extract the revenue using R functions. Please help.
You could do
df <- read.table(header=T,text="Revenue
3.2
5.4
7.6
9.2")
df$ExRevenue <- c(df$Revenue[1], diff(df$Revenue))
df
# Revenue ExRevenue
# 1 3.2 3.2
# 2 5.4 2.2
# 3 7.6 2.2
# 4 9.2 1.6

rounding of numbers using R

I want to convert the following numbers in this way I tried to use all possible methods but i am unable to get the value which i expected
value round off value
0.0 - 4.9 0
5.0 - 5.9 6
6.0 - 6.9 7
7.0 - 7.9 8
8.0 - 8.9 9
9.0 - 10.0 10
The above table is for reference
expected output eg :- roundup(5.0) = 6 ,roundup(6.9)=7
You can try:
roundup<-function(x) c(0,6:10)[findInterval(x,c(0,5:9))]
roundup(c(5,6.9))
#[1] 6 7

non-comprehensible modulo calculation [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 6 years ago.
I've got a strange result for my modulo query here. Maybe somebody has a solution for it:
d <- seq(0.0,1.0,0.1)
lab.y <- ifelse(((d*10) %% 2.0 == 0.0),d, NA)
will give the result:
[1] 0.0 NA 0.2 NA 0.4 NA NA NA 0.8 NA 1.0
so the 0.6 is missing.
I tried to add a query like:
ifelse((d*10/2 == 3.0), d, NA)
which is all FALSE even though
d*10/2
[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
...
I don't really understand what's going on here.
Thanks a lot in advance!
This is due to floating point errors, you should look for low absolute differences instead of exact matches. It is not 0.6, but (just an example) 0.6000000003 or 0.5999999997. Try something like:
ifelse((abs((d*10) %% 2)<0.000001), d, NA)

Extracting complete paired values (non-NA) from a matrix in R [duplicate]

This question already has answers here:
Remove rows with all or some NAs (missing values) in data.frame
(18 answers)
Closed 7 years ago.
I apologize if this is elementary or has been answered before, but I haven't found an answer to my question despite extensive searching. I'm also very new to programming so please bear with me here.
I have a bunch of 25 by 2 matrices of data, however some of the cells have NA values. I'm looking to extract a subset of the matrix consisting of only the complete paired values (so no NA values).
So say I have:
3.6 4.2
9.2 8.4
4.8 NA
1.1 8.2
NA 11.6
NA NA
2.7 3.5
I want:
3.6 4.2
9.2 8.4
1.1 8.2
2.7 3.5
Is there some function that would do this easily?
Thanks!
Try this
df <- read.table(text = "3.6 4.2
9.2 8.4
4.8 NA
1.1 8.2
NA 11.6
NA NA
2.7 3.5")
df[complete.cases(df), ]
# V1 V2
# 1 3.6 4.2
# 2 9.2 8.4
# 4 1.1 8.2
# 7 2.7 3.5
df[ apply(!is.na(df), 1, all) , ]
df <- data.frame(V1 = c(3.6,9.2,4.8,1.1,NA,NA,2.7),
V2 = c(4.2,8.4,NA,8.2,11.6,NA,3.5))
EDIT: I forgot na.omit or complete.cases Doh.

generate an output from a calculation between 2 columns in R

I have a data set representing movement through a 2d environment with respect to time:
time(s) start_pos fwd_dist rev_dist end_pos
1 0.0 4.0 -3.0 2.0
2 2.0 5.1 0.5 3.0
3 3.0 4.7 -0.5 3.5
4 3.5 3.6 -1.8 2.1
5 2.1 2.6 -2.1 1.0
6 1.0 1.5 -1.5 -0.2
I want to make another column which is the result of a check to see which is larger between "end_pos" and "start_pos" and subtracting the larger number from "fwd_dist". I'm trying to loop through the dataset but seem to be struggling with the syntax in R
i<-0
while (i < length(data[,1]){if (data[i,4] > data[i,1]){print (data[i,2]-data[i,4])} else {print (data[i,2]-data[i,1])}; i<-i+1}
I keep getting the error:
Error in if (data[i, 4] > data[i, 1]) { :
argument is of length zero
pmax(start_pos,end_pos)
will give you the parallel maximum (i.e., componentwise) of two vectors. So you are probably looking for
fwd_dist-pmax(start_pos,end_pos)
A data frame based approach:
data$difference <- data$fwd_dist - pmax(data$start_pos, data$end_pos)

Resources