[Solved] sum lines with similar substring [closed]

We could match the substring starting from _ to the end of the string (.*$) in ‘IsomiR’ column and replace with ” using sub. We use that as the grouping variable. If we are doing this with dplyr, the summarise_each can be used for summing multiple columns. library(dplyr) df1 %>% group_by(IsomiR= sub(‘_.*$’, ”, IsomiR)) %>% … Read more

[Solved] Highlight specific genes in boxplot

I’ll give you a ggplot2 answer. For this, you need to reshape your data, so there are separate x and y variables. Right now, your y values are split between two columns. Then we highlight the specific genes by only plotting points for a subset. library(ggplot2) library(dplyr) library(tidyr) gene_list <- c(‘C’, ‘F’, ‘G’, ‘I’) df_long … Read more

[Solved] calculating mean for part of the data in R

> apply(x, 2, function(z) mean(z[order(z)][1:2]) ) [1] 1.5 6.5 Look at ?apply for row-wise or column-wise operations on matrices or all-numeric or all-character subsets of dataframes. This is actually the lowest 40% of values, since 2/5 = 0.4. If you wanted this to be “sensitive” to varying lengths (and still asking for 40% rather than … Read more

[Solved] R: Splitting a string to different variables and assign 1 if string contains this word [duplicate]

First off, for future posts please provide sample data in a reproducible and copy&paste-able format. Screenshots are not a good idea because we can’t easily extract data from an image. For more details, please review how to provide a minimal reproducible example/attempt. That aside, here is a tidyverse solution library(tidyverse) df %>% separate_rows(Text, sep = … Read more

[Solved] Object not detected in R [closed]

There are two ways: 1) Attaching the data: attach(data1) And then this code should work: cor(V1, V2) 2) Using $ for accesing columns in a dataframe cor(data1$V1, data1$V2) solved Object not detected in R [closed]

[Solved] ggplot Grouped barplot with R

You can try library(tidyverse) d %>% gather(key, value, -PtsAgRdTm) %>% ggplot(aes(x=PtsAgRdTm, y=value, fill=key)) + geom_col(position = “dodge”) You transform the data from wide to long using tidyr’s gather function, then plot the bars in a “dodge” or a “stack” way. 3 solved ggplot Grouped barplot with R

[Solved] ggplot chart, x=date, y= value, value

d <- read.table(text=readClipboard(), header=TRUE, stringsAsFactors = T, na.strings=”U”) df <- melt(d, id.var=”date”) ggplot(aes(x=date, y=value), data = df) + geom_bar(aes(fill = variable), stat=”identity”, position = ‘dodge’) or ggplot(aes(x=factor(date), y=value), data = df) + geom_bar(stat=”identity”, position = ‘dodge’) + facet_grid(variable~., scales=”free_y”, drop = F) + theme(axis.text.x = element_text(angle = 45, vjust = 1.1, hjust = 1.05)) 4 … Read more

[Solved] R: Remove list type within dataframe

Try to do this library(‘stringr’) apply(data, 1, function(x) str_c(x$columnnane,collapse=”,”)) where, data is you dataframe and columnname is the column containing list. edited answer out = do.call(rbind, lapply(data, function(x) str_c(x,collapse=”, “))) where data is your list object if the list is stored inside a dataframe then pass the column in place of the data above like … Read more

[Solved] Look up from different dataframes depending on a column

Here’s one way to add a new column by reference using data.table: require(data.table) setDT(d1); setDT(d2); setDT(data) # convert all data.frames to data.tables data[src == “one”, location := d1[.SD, location, on=”index”]] data[src == “two”, location := d2[.SD, location, on=”index”]] .SD stands for subset of data, and contains all columns in data that matches the condition provided … Read more

[Solved] R: cbind dataframes with common column names

Not sure what is your question, but you can specify your own column prefix for columns of each merged object with named arguments of cbind: data(‘cars’) cars2=cbind(DataSet1=cars, DataSet2=cars) head(cars2) # DataSet1.speed DataSet1.dist DataSet2.speed DataSet2.dist # 1 4 2 4 2 # 2 4 10 4 10 # 3 7 4 7 4 # 4 7 … Read more

[Solved] max() function gives wrong output [closed]

Because you are comparing character strings. Consider… max(“apple”,”banana”,”banana2″) #[1] “banana2” max( “1” , “2” , “10” ) #[1] “2” sort( c( “1” , “2” , “10” ) ) #[1] “1” “10” “2” sort( as.integer( c(“1” , “2” , “10” ) ) ) #[1] 1 2 10 max( as.integer( c(“1” , “2” , “10” ) ) … Read more