-4

For example, I have a data.frame with 40 rows and 20 columns and want to create 100 variables assigned to the name of the first row (a string):

row_name_1 <- df[1, ]

Is there a way to write a loop to do this for all 100 rows that would save the trouble of typing 40 lines of code?

I have tried using this code:

Phoneme_Features.list <- setNames(split(Phoneme_Features,
          seq(nrow(Phoneme_Features))), rownames(Phoneme_Features)) 

The specific application for this would be to be able to search another data frame based on the values from the first data frame.

I have 2 data frames: Phoneme_Features and Phonetic_Dictionary (with 130,000 rows). Phoneme features is data frame where each row corresponds to around 20 phonetic features (e.g. F = consonant = 1, vowel = 0, labial = 1, dental = 1, etc). The Phonetic_Dictionary contains 130,000 words with the corresponding phonetic transcription (e.g. phonetics F AH0 N EH1 T IH0 K S) I want to use the new variables to replace the values of another data frame (stored as factors) so that I can search items in the second data frame by the features in the first data frame (Phoneme Features).

I would like to be able to search Phonetic_Dictionary and return every entry in which the first column contains a value of 1 for consonant. In other words, to be able to search the dictionary for all entries with an initial consonant, or final high vowel, or any other feature from the first data frame Phoneme_Features.

Phono
  • 65
  • 1
  • 6
  • Welcome to SO. You may find you get a better response if your produce a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your problem. It demonstrates the effort you've put in to research and solve – Kevin Arseneau Nov 12 '17 at 22:28
  • Can you show us what you've attempted? – Sam Nov 12 '17 at 22:28
  • Thanks! This is what I attempted so far, based on some other SO questions searches: > Phoneme_Features.list <- setNames(split(Phoneme_Features, seq(nrow(Phoneme_Features))), rownames(Phoneme_Features)) > Phoneme_Features.list But it did not give me the results I needed. I'm quite new to R and programming in general. – Phono Nov 12 '17 at 22:57
  • From your description of the background of your question, your actual problem is not to split a data frame into separate variables but to look up a second data frame by a key given in the first data frame, right? Then you might have to look for "merge" or "join". Please, [edit] your question and post the result of `dput(head(Phoneme_Features))` and likewise for `Phonetic_Dictonary` or give a link to it, please. Without a [mcve] it is difficult to give a proper answer. – Uwe Nov 13 '17 at 07:06

2 Answers2

3

You can use assign() and paste0() to create variables names programatically.

An example using the iris dataset:

for(i in 1:nrow(iris)){
  assign(paste0('row_name_',i),iris[i,])
}

paste0() attaches the row number, i, to the string row_name_ and then assign() then assigns the newly created variable name to the enviroment with a value of iris[i,]

Andrew Haynes
  • 2,612
  • 2
  • 20
  • 35
  • Thanks! This works great to create each variable. However, it doesn't add create the variable names that I'm looking for. Is there a way to create a variable that is the value in the first column of my data frame. In my case the first column consists of a string, e.g. Column_1 = (AA, AH, EY, ... n ~ 40) and I want each new variable to be AA <- (Columns 2:20). – Phono Nov 12 '17 at 23:12
  • 1
    `fortunes::fortune(236)`: *The only people who should use the assign function are those who fully understand why you should never use the assign function.* (Greg Snow, R-help, July 2009) – Uwe Nov 13 '17 at 08:31
0

Thanks for everybody's help. I was able to get what I wanted by using:

for(i in 1:nrow(Phoneme_Features)){
assign(paste0(Phoneme_Features[i, ]), Phoneme_Features[i, ])}
Phono
  • 65
  • 1
  • 6
  • Why do you think this is not the same as @andrew-haynes answer? – Kevin Arseneau Nov 13 '17 at 01:37
  • 1
    It is essentially his answer, which was almost exactly what I was looking for. This is different in that instead of creating a new variable called Row_Name_i (where i ranges from 1-40 for each of the 40 rows, it creates new variables where the name is the string value in the first column of that row. – Phono Nov 13 '17 at 01:50