Mangoplant
Mangoplant

Reputation: 11

How to split Columns by Row Value

I am learning R and had a simple question that I was curious about. I have a data frame like the one below:

Sample ID:        A  B  C   D   E   F
Value1            8  3  4   8   9   2
Value2            8  1  7   7   6   1
Value3            9  3  5   8   3   2

I would like to split the columns in this dataframe by their Value3. For example, I would like to put all samples that have a Value3 of greater than 4 into a separate dataframe. So, for example, I would like to get this in the new dataframe (all samples with Value3 > 4):

Sample ID:      A   C   D
Value1          8   4   8
Value2          8   7   7
Value3          9   5   8

Is there a simple way to go about doing this in R? Here is what I have tried but does not seem to work:

library(tidyverse)    

data <- read.csv("sampledata.csv")
filtered_data <- filter(data, Value3 > 4)

Any advice would be much appreciated!

Upvotes: 1

Views: 51

Answers (2)

G5W
G5W

Reputation: 37631

A simple option would be to use:

df2 = df1[, which(df1["Value3",] > 4)]

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388797

One option would be using split.default in base R. We first subset the row where SampleID is "Value3", compare value with 4 and get list of two dataframes one with value greater than 4 and another with value less than 4.

split.default(df[-1], df[df$SampleID == "Value3", -1] > 4)

#  B E F
#1 3 9 2
#2 1 6 1
#3 3 3 2

#$`TRUE`
#  A C D
#1 8 4 8
#2 8 7 7
#3 9 5 8

data

df <- structure(list(SampleID = structure(1:3, .Label = c("Value1", 
"Value2", "Value3"), class = "factor"), A = c(8L, 8L, 9L), B = c(3L, 
1L, 3L), C = c(4L, 7L, 5L), D = c(8L, 7L, 8L), E = c(9L, 6L, 
3L), F = c(2L, 1L, 2L)), class = "data.frame", row.names = c(NA, -3L))

Upvotes: 0

Related Questions