how to compare values in a R data frame - r

I am trying to work with the below data frame and what I am trying to do is to compare the values from columns:
#create a sample data frame
df<-data.frame(
item=c("a","b","c","d"),
price_today=c(1,2,3,4,5),
price_yesterday=c(1,2,3,4,5)
If values from column price_today are the same compared with values from column price_yesterday, then print okay or else print not okay, and the printed result will be shown as a new variable in the data frame.
May I know how should I go about the ifelse part here?
Many thanks for your help and have a good day.
Modified Questions:
Hi all, so now what if the df becomes like this:
#create a sample data frame (modified)
df<-data.frame(
item=c("a","a","c","d"),
price_today=c(1,"",3,"XYZ",5),
price_yesterday=c(1,2,3,4,5)
Now it contains both blank value and non-numerical values in column price_today. And instead of a,b,c,d in column item, it becomes a,a,c,d in column item. I have been trying to do the following:
Sort column item by "a" and I have the below code:
df_1<-df[df$item=="a",]
After df_1 is filtered, then again sort price_today, by removing blank and non-numerical values with codes below:
df_1<-df[!is.numeric(df_1$price_today),]
I am able to filter out by "a" in column item, however, with the second filter, it then returns with the original df, may I know what did I do wrong here?
Million thanks for your help and have a good day/night.

Related

Filter list with multiple objects with a column condition in r

I have a large list with 13 elements and each element has 181 rows and 31 columns. I am trying to filter each element based on one specific column. For example, I want to filter the elements' rows based on the column named X with the following condition X<=0.30. If it was a data frame, for instance, I would do "filter(X<=0.30)".
Here are some of the codes I tried where "out" is my data frame and "X" is the variable with the condition (X<=0.30):
out_new<-(list.filter(out,X<=0.30))
out_new<-out[sapply(out, X)<=0.30]
However these codes return a list of 0. I am not sure what I am doing wrong or missing. So any help is very much appreciated. Thank you.
I am presuming u have a list of data-frame and you want to filter each df based on one specific column.
my_function<-function(df){
df<-df %>% filter(X<=0.30)
}
map(list_name,my_function)

How to delete column name of single column of data frame?

I am working on data set for analysis.
I have to remove column name of single variable of data frame in r.
I have used colnames(df)<-NULL function,it removes all colnames of dataframe
The excepted output is:
I don't really get the expected output, but maybe this is what you're looking for
colnames(df)[i] <- NA
'i' is the column location (for example 1,2,3 etc)

Extract the last column from a list of data frames

I have a list with ten data frames with different number of columns and I would like to get a list with ten vectors. Each vector would be the last column from each data frame.
If I want to get the last column from the first data frame, I run:
lapply(list_with_df,function(x) x[,lengths(list_with_df)[1]])
But I have got the same column number for each data frame.
I have tried to do a "loop for" through the ten data frames, but I have got an error. I appreciate if someone could help me with this matter. Regards.
Instead of using the lengths you can ask of the number of columns by ncol. This should work:
lapply(list_with_df,function(x) x[,ncol(x)])
Edit
Just for some clarification: The reason why you got the same column number for each data frame is because you have always selected the column number according to the first element of lengths vector by using lengths(list_with_df)[1]. It was always the length of the first data.frame

Create Data Frame from IF Condition

I've look for this question; however no examples seem to be as primitive as I need.
I need to be able to create a data frame that contains only the true values from an IF condition.. for example:
if(sampleDF$column[1]== 1){
newDF(sampleDF)
}
In this example newDF should contain those rows from sampleDF where column has a value of 1
Thanks in advance!

Aggregate rows by identical column values for multiple data type columns

I am trying to aggregate rows with identical ObservationID for the following data:
https://www.dropbox.com/s/nd29kx2j7vik3tj/data.stackoverflow.question.25.11.2016.txt?dl=0
enter image description here
In the picture: >data is the original data, and at the top it shows how I would like to look my data like after aggregating
I tried:
data1<-aggregate( .~ ObservationID,data=data,FUN= paste)
which returns numbers instead of names for some of the columns, and places NA over a cell with an actual value (see picture)
and I tried:
data2<-aggregate(x=data,by=list(data$ObservationID),FUN = na.omit)
which returns two columns for every one column that wat put in.
On the top of the picture, I show how I would like to my data to be returned. Can someone help?
I hope someone can help me!
Thanks

Resources