logo
down
shadow

Remove columns using column name based on levels of factors


Remove columns using column name based on levels of factors

By : Payal Haldar
Date : November 22 2020, 02:59 PM
Hope that helps We use sub to remove the prefix x. from the column names of 'x', check whether it is %in% the 'f' column to create a logical vector and use this to subset the columns of 'x'. We removed the first column name (as it is 'f') and later concatenated with TRUE to include that column also in the subset.
code :
 x[c(TRUE,sub('.*\\.', '', names(x)[-1]) %in% x$f)]
x[c(TRUE,grepl(paste(x$f, collapse='|'), names(x)[-1]))]


Share : facebook icon twitter icon
How to combine two columns of factors into one column without changing the factor levels into number

How to combine two columns of factors into one column without changing the factor levels into number


By : Abhinav M Balraj
Date : March 29 2020, 07:55 AM
hop of those help? factors are numbers that happen to have labels. When you combine factors, you generally are combining their numeric values. This can often trip a person up.
If you want their labels, you must coerce them to strings, using as.character
code :
 student.list <- c( as.character(dataset1[,2]) ,
                    as.character(dataset2[,2])  )
 student.list <- c(as.character(dataset1[,2]),as.character(dataset2[,2]))
 student.list <- as.factor(student.list)
Aggregate data in one column based on values/factors in four another columns

Aggregate data in one column based on values/factors in four another columns


By : Basem Hegazy
Date : March 29 2020, 07:55 AM
To fix this issue I have a data frame in my R script that looks something like this: , Use the dplyr package in R.
code :
species.prop %>% group_by(species, area, month, year) %>% summarise(catch.p = sum(catch.p))
  species   area month  year catch.p
   (fctr) (fctr) (dbl) (dbl)   (dbl)
1     hom     IV     4  1998   27.60
2     hom     VI     4  1998   17.50
3     hom     VI     4  2000   40.25
4     pil     VI     4  1998    8.05
5     pil    VII     1  2000   46.00
6     pil    VII     5  1998   17.50
  species   area month  year catch.p
   (fctr) (fctr) (dbl) (dbl)   (dbl)
1     hom     IV     4  1998   27.60
2     hom     VI     4  1998   17.50
3     hom     VI     4  2000   40.25
4     pil    VII     1  2000   46.00
5     pil    VII     5  1998   25.55
Change column values based on factors of other columns

Change column values based on factors of other columns


By : Max Salagor
Date : March 29 2020, 07:55 AM
With these it helps For this particular example, since your factors and years are both ordered and incremented by 1, you could just subtract 2007 from the year column and multiply it by profit.
code :
transform(df, profit = profit * (year - 2007))
#   profit year
# 1     30 2010
# 2     40 2011
# 3     50 2012 
lookup <- c("2010" = 3, "2011" = 4, "2012" = 5)
transform(df, profit = profit * lookup[as.character(year)])
#   profit year
# 1     30 2010
# 2     40 2011
# 3     50 2012
for(i in seq_len(nrow(df))) {
    df$profit[i] <- with(df, switch(as.character(year[i]),
        "2010" = 3 * profit[i],
        "2011" = 4 * profit[i],
        "2012" = 5 * profit[i]
    ))
}
Restructuring dataframe from multiple columns of factors to one column containing factors

Restructuring dataframe from multiple columns of factors to one column containing factors


By : Robert Amor
Date : March 29 2020, 07:55 AM
seems to work fine We can use the gather function from the . starts_with is a function from that can select the columns start with a string.
code :
library(dplyr)
library(tidyr)

dat2 <- dat %>%
  gather(Factor, Factor_population, starts_with("Factor_"))

dat2
#     Clone.ID  Sequence   Factor Factor_population
# 1  clonename  seq_data Factor_1                 1
# 2 clonename2 seq_data2 Factor_1                 2
# 3  clonename  seq_data Factor_2                 5
# 4 clonename2 seq_data2 Factor_2                 1
# 5  clonename  seq_data Factor_3                 4
# 6 clonename2 seq_data2 Factor_3                 3
dat <- read.table(text = "'Clone ID'    Sequence    Factor_1    Factor_2    Factor_3

clonename   seq_data       1            5          4

clonename2  seq_data2      2            1          3",
                  header = TRUE, stringsAsFactors = FALSE)
R change factors levels of a variable, and remove old ones

R change factors levels of a variable, and remove old ones


By : sergio_sos
Date : March 29 2020, 07:55 AM
I hope this helps . I have a large data set, which is read from SPSS file. It contains several rows and columns, read from many small SPSS files. The SPSS file contained some mistakes, which I want to correct in R. When the data is read, it and has all noises in factor levels, but data is ok in SPSS. I cannot change factor levels in many individual files in SPSS. Following is the small sample of data that I have ,
Clean your data. This will only leave numbers and NA.
Related Posts Related Posts :
  • Remove same columns from left_join
  • Grouped barplot with R
  • Strange addTaskCallback work in RStudio
  • Make dual X-axs based on different variables using ggvis
  • Generate a normal distribution within certain limits in R
  • from data table, randomly select one row per group
  • ngrams not in correct order
  • ggplot: plot title and plot overlap each other
  • Display groups with different borders in histogram with panel.superpose
  • Submit form with no submit button in rvest
  • Can't download .csv from dropbox
  • How to get Adjusted R-Square value as variable?
  • Check row-wise if element exists in comma-separated column with position
  • How to know which nodes in a graph are removable
  • Generate all possible permutations of a binary matrix
  • R, Windows and foreign language characters
  • Datapusher with missing values and mixed (srting/numeric) data
  • Adding greek characters with variables to axis title
  • R - Subsetting subsets of variable names in loops
  • R dynamically naming a data frame to be used in ggplot2
  • Optimize/vectorize a loop in R that generates randoms from ranges in input vectors?
  • How to sort groups within sorted groups?
  • How to get randomForest model output in probability using Caret?
  • how to plot a data frame into R
  • Calculate mean of a range of rows
  • How to sum the values in a text file?
  • Looking up polygons in a shapefile that point belong in... why does it work with some shapefiles, not with others?
  • R creating a list using variable columns from df
  • Determine programmatically if a function call refers to your own function or a package's (or base R's) function?
  • Adding Special Characters to a plot in R
  • How to dynamically change R .libPaths() based on hostname?
  • Print arguments of a function in R
  • Creating Variables via A loop in r
  • How to randomly divide an integer into a fixed number of integers, such that the obtained tuples are uniformly distribut
  • What units are the 'width = ' in geom_bar(aes = ) and position_dodge(width = ) rendered in?
  • Assigning Dates to Fiscal Year
  • conditional removal from data frame
  • Getting variables out of a function in R
  • Bar colors in ggplot2 geom_bar not applied correctly (R)
  • Create a vector in a dataframe based on matching a second vector to a row name in another object
  • Convert julian day to mm/dd and add year from another column in dataframe in r
  • Change SMOTE parameters inside CARET k-fold cross-validation classification
  • rollapply classes each segment the same
  • rename the header of the spliting group in r
  • Summary stats by factor level for multiple variables
  • Processing Rmarkdown inside chunks
  • R: how to join the duplicate rows in one dataframe
  • Creating a unified time-series, with dates coming from different (natural) languages
  • R error: attempt to apply non-function , randomForest , rfImpute
  • assign multiple color to each vertex in igraph
  • How do I add shading and color to the confidence intervals in ggplot 2 generated Kaplan-Meier plot?
  • How to generate spatial points with a pattern
  • ggplot2 + stat_contour variable binwidth
  • Add fake data to a data frame based on variable condition
  • Utilizing a character vector inside a function
  • match fundction with data frames that are differently constructed
  • Overlaying plots with a horizontal date in R
  • lapply'ing a for loop returns NULL
  • Dynamically add UI elements and gather their input in a dataframe in shiny
  • All the possible outcomes
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co