Qiang Li
Qiang Li

Reputation: 10855

process text file in r to get two vectors

I have a text file whose format is like following:

groupA, 123
groupA, 32
groupB, 222
groupA, 212
groupB, 213
...

How can I easily form two vectors [123, 32, 212, ...] and [222, 213, ...] for groupA and groupB each?

Thank you.

Upvotes: 0

Views: 67

Answers (2)

Ben
Ben

Reputation: 42283

Ananda Mahto's answer is more versatile (and interesting!), but here's a simple sub-setting method that also gets the two vectors:

mydf <- read.csv(header = FALSE, text = "groupA, 123
groupA, 32
groupB, 222
groupA, 212
groupB, 213")

# the dataframe is automatically assigned
# column names V1 and V2...

# vector of V2 for groupA
mydf[mydf$V1 == "groupA",]$V2
[1] 123  32 212

# vector of V2 for groupB
mydf[mydf$V1 == "groupB",]$V2
[1] 222 213

Upvotes: 0

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

You can simply use split. This will result in a named list with each list item being one of your vectors.

mydf <- read.csv(header = FALSE, text = "groupA, 123
groupA, 32
groupB, 222
groupA, 212
groupB, 213")
mydf2 <- split(mydf$V2, mydf$V1)
mydf2
# $groupA
# [1] 123  32 212
# 
# $groupB
# [1] 222 213

If you don't want a list (though I find it preferable to having lots of single vector objects in my workspace), you can use lapply and assign:

## Verify that there are no objects currently named `groupA` or `groupB`
ls(pattern = "group")
# character(0)

## Assign each list item to a new object, making use of the `names` of the list
invisible(lapply(names(mydf2), 
                 function(x) assign(x, mydf2[[x]], envir=.GlobalEnv)))

## Verify the presence and values of the new vectors
ls(pattern = "group")
# [1] "groupA" "groupB"
groupA
# [1] 123  32 212
groupB
# [1] 222 213

Upvotes: 1

Related Questions