0

I am trying to make a sub dataframe based on the already existing dataframe. My sub dataframe is being filled with the number of the row instead of the row itself.

rates = read.csv("file.txt")
genes = unique(gsub('_[0-9]+', '', rates[,1]))
for (k in unique(gsub('_[0-9]+', '', rates[,1])) ){
        sub = print(grep(k, rates[,1]), value=T)
        sub
}

file.txt

clothing,freq,temp
coat_1,0.3,10
coat_1,0.9,0
coat_1,0.1,20
coat_2,0.5,20
coat_2,0.3,15
coat_2,0.1,5
scarf,0.4,30
scarf,0.2,20
scarf,0.1,10

This is what is currently output

[1] 1 2 3 4 5 6
[1] 7 8 9

I would like something like this instead

  clothing freq temp
1   coat_1  0.3   10
2   coat_1  0.9    0
3   coat_1  0.1   20
4   coat_2  0.5   20
5   coat_2  0.3   15
6   coat_2  0.1    5

  clothing freq temp
1    scarf  0.4   30
2    scarf  0.2   20
3    scarf  0.1   10
3
  • 1
    You could just do split(rates, rates$clothing == "scarf") Commented Jul 22, 2017 at 1:06
  • 1
    Try this instead in the loop: sub = print(rates[grep(k, rates[,1]),] ) Commented Jul 22, 2017 at 1:15
  • Thanks @Dave2e , that did it Commented Jul 22, 2017 at 1:19

1 Answer 1

1
 rates <- read.csv("file.txt", stringsAsFactors = FALSE)
 rates
#  clothing freq temp
# 1   coat_1  0.3   10
# 2   coat_1  0.9    0
# 3   coat_1  0.1   20
# 4   coat_2  0.5   20
# 5   coat_2  0.3   15
# 6   coat_2  0.1    5
# 7    scarf  0.4   30
# 8    scarf  0.2   20
# 9    scarf  0.1   10

 rates[rates$clothing != "scarf",]
#  clothing freq temp
# 1   coat_1  0.3   10
# 2   coat_1  0.9    0
# 3   coat_1  0.1   20
# 4   coat_2  0.5   20
# 5   coat_2  0.3   15
# 6   coat_2  0.1    5
rates[rates$clothing == "scarf",]
#  clothing freq temp
#7    scarf  0.4   30
#8    scarf  0.2   20
#9    scarf  0.1   10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.