logo
down
shadow

Using lapply and which to subset dataframe by both characteristic and fuction


Using lapply and which to subset dataframe by both characteristic and fuction

By : Relja Trkulja
Date : November 18 2020, 03:01 PM
wish help you to fix your issue for the advice. I ended up going with ddply in order to get my data into a more usable format, following general advice from this post.
Here's the simple example:
code :
> A=data.frame(seq(1,9),rep(c("a","b","c"),3),c(rep("type1",3),rep("type2",3),rep("type3",3)),c(rep("place1",2),rep("place2",2),rep("place3",2),rep("place1",2),rep("place2",1)))
> names(A)=c("value","Letter","Type","Place")
> A
  value Letter  Type  Place
1     1      a type1 place1
2     2      b type1 place1
3     3      c type1 place2
4     4      a type2 place2
5     5      b type2 place3
6     6      c type2 place3
7     7      a type3 place1
8     8      b type3 place1
9     9      c type3 place2
> sub=ddply(A[which(A$Place=="place1" & A$Type=="type1"),],"value",summarize,mean=mean(value,na.rm=T))
> sub
  value mean
1     1    1
2     2    2
> head(alldata)
        value layer Kmultiplier Resolution      Season           Variable
1: 0.00000000     b           1        1km    Baseflow Evapotranspiration
2: 0.01308008     b         .01        1km    Baseflow Evapotranspiration
3: 0.00000000     b           1        1km   Peak Flow Evapotranspiration
4: 0.03974779     b         .01        1km   Peak Flow Evapotranspiration
5: 0.00000000     b           1        1km Summer Flow Evapotranspiration
6: 0.02396524     b         .01        1km Summer Flow Evapotranspiration
  for(j in Season){
    for(i in res){
      ET=ddply(alldata[which(alldata$Variable=="Evapotranspiration" & alldata$Resolution==sprintf("%s",i) & alldata$Season==sprintf("%s",j)),],"Kmultiplier", summarize, mean = mean(value,na.rm=T))
      ET$Variable="Evapotranspiration";ET$Resolution=sprintf("%s",i);ET$Season=sprintf("%s",j)
      S=ddply(alldata[which(alldata$Variable=="Change in Storage" & alldata$Resolution==sprintf("%s",i) & alldata$Season==sprintf("%s",j)),],"Kmultiplier", summarize, mean = mean(value,na.rm=T))
      S$Variable="Change in Storage";S$Resolution=sprintf("%s",i);S$Season=sprintf("%s",j)
      Q=ddply(alldata[which(alldata$Variable=="Discharge" & alldata$Resolution==sprintf("%s",i) & alldata$Season==sprintf("%s",j)),],"Kmultiplier", summarize, mean = mean(value,na.rm=T))
      Q$Variable="Discharge";Q$Resolution=sprintf("%s",i);Q$Season=sprintf("%s",j)
      if(i=="1km"){resbind=rbind(Q,S,ET)}else{resbind2=rbind(resbind,Q,S,ET)}
    } 
    if(j=="Baseflow"){sbind=rbind(resbind2,Q,S,ET)}else if(j=="Peak Flow"){sbind2=rbind(resbind2,sbind,Q,S,ET)}else{ETSQ=rbind(resbind2,sbind2,Q,S,ET)}
  }
  ETSQ$Variable=factor(ETSQ$Variable,levels=c("Change in Storage","Evapotranspiration","Discharge"))
  print(ggplot(data=ETSQ,aes(x=Kmultiplier,y=mean, color=Variable,group=Variable))
        +geom_point()
        +geom_line()
        +labs(x="K scaled by",y="Percent change from Baseline case")
        +scale_y_continuous(labels=percent)
        +facet_grid(Season~Resolution)
        +theme_bw()
  )
  ggsave(sprintf("%s/Plots/SimpleLines/Variable_by_K.png",path),device = NULL,scale=1)


Share : facebook icon twitter icon
Using lapply to subset a list of character lists in R

Using lapply to subset a list of character lists in R


By : user3090680
Date : March 29 2020, 07:55 AM
wish of those help I have the following line that subsets a character list correctly: , You could try
code :
lapply(cpc_data2, function(x) x[x!=''])
Python Pandas: how to overwrite subset of a dataframe with a subset of another dataframe?

Python Pandas: how to overwrite subset of a dataframe with a subset of another dataframe?


By : user3919583
Date : March 29 2020, 07:55 AM
will help you You are looking for the DataFrame.loc method
Small example:
code :
import pandas as pd
df1 = pd.DataFrame({"data":[1,2,3,4,5]})
df2 = pd.DataFrame({"data":[11,12,13,14,15]})
df3 = df1.copy()
df3.loc[3:4] = df2.loc[3:4]

df3
   data
0     1
1     2
2     3
3    14
4    15
subset a dataframe using condition passed as string (subset dataframe dynamically)

subset a dataframe using condition passed as string (subset dataframe dynamically)


By : jmonsuarez
Date : March 29 2020, 07:55 AM
With these it helps We can retrieve a subset of data-frame df where col1's value is equal to 1 using the following code. , Try:
code :
cond = "df$col1==1"
sub = df[eval(parse(text=cond)),]
Subset data frame using lapply and subset

Subset data frame using lapply and subset


By : user1652284
Date : March 29 2020, 07:55 AM
it fixes the issue You can do this with base R and no loops. Create a splitting variable f and then split the data frame.
code :
d <- diff(c(0, k_day_regressions))
f <- rep.int(rep(1:length(d)), times = d)
dataraw.samples <- split(dataraw.1, f)

dataraw.samples[[1]]
#  ABEV3 AEDU3 ALLL3 BBAS3
#1 15.20  9.01  7.71 22.85
#2 14.90  8.56  7.81 22.78
#3 15.22  8.66  7.57 22.80
#4 15.15  8.64  7.27 22.22
#5 15.18  8.44  7.29 22.51
Using lapply and !is.na to subset list vectors in R

Using lapply and !is.na to subset list vectors in R


By : sagar surwade
Date : March 29 2020, 07:55 AM
it fixes the issue several different ways to go about it. A simple approach is with an anonymous function:
Related Posts Related Posts :
  • how to loop for division funciton in r
  • Why does ggplot not allow suppressing of messages generated by its geoms?
  • Download multiple excel files linked through urls in R
  • sparklyr : spark_apply function is not working in cluster mode
  • dplyr mutate - How do I pass one row as a function argument?
  • R selecting rows by conditions given in an external table
  • Native regex way to replace multiple leading chars with equal number spaces
  • stan - difficulty vectorizing
  • How to define a function that calls shiny functions?
  • How to count number of observations in a "n" dimensional range in R
  • Superimposing asymmetric t-distribution using ggplot2
  • Makefile to render all targets of all .Rmd files in directory
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org