r - Generate strings using data in a data.frame, using the colnames, and combining the data in different ways based on the value -
I have a data frame that I would like to use to create a SQL query.
Clause of my data:
data & lt; - Structure (list (inclass = c ("01", "99", "99"), child = c ("0", "2" ("1", "1", "1"), .names = C ("inclass", "4"), high_edu = c ("00", "00", "14"), ref_race = c ("1", "1", "child", "high_ADU", "riff_res "), Line.nooy = C (1 L, 2L, 3L), class =" data.frame ") I need to create all the data based on rows Various types of phrases, where the value '99' behaves differently.
Phrase1: If the value is '99' then it should say "99" column As the name "otherwise it is not only the column
Then the second line will look like this:
'99' asclass, childage, high_ADU, riff_res < P> Phrase2: If it is not '99' then combine the column name with the value. It would look like for the second line:
save = '2', paste the column names, where the value is '99'
< P> I find it hard to figure out what the value is / is not '99' edit
I think my question might be a bit confusing. I am trying to get these three phrases for each line here, which is probably the data that will make it clear what I am trying to do. Structure (list (inclass = c ("01", "99", "99", "1" "2"), child = C ("0", "99",
"4", "6", "3"), high_agu = c ("00", "99", "14", "99" "99"), ref_race = c ("1", "1", " '99' high_edu, ref_race ", '99', as 'childage' in the form of '99', '1', '99', '4'), phrase 1 = c (" inclass, childage, high_edu, ref_race "," 99 "inclass In the form of '99' inclass, as childage, high_edu, ref_race "," inclass, childage, '99' as high_edu, '99' as ref_race, "inclass, childage, 99 "high_edu, ref_race"), phrase2 = c ("inclass = '01', childage = '0', high_edu = '00', ref_race = '1'", "ref_race = '1'" "childage = '4', high_edu = '14', ref_race = '1' "," inclass = '1', childage = '6' "," inclass = '2', childage = '3', ref_race = '4' "), Phrase3 = c (" inclas S, childage, high_edu, ref_race "," ref_race "," childage, high_edu, ref_race "," inclass, childage "," inclass, childage, ref_race ")). Name = c (" class "," child " "High_ADU", "riff_res", "phrase1", "phrase2", "phrase3"), line.Name = c (NA, 5L), class = "data.frame") I believe this should solve your problem. It can not be the most beautiful, but I think it is the least straightforward. You want to see in the works like any and when you want specific output like this then get familiar with paste basically, all I am doing is that Column names are found where the value is either '99' or not. So it's a matter of using the paste properly.
For (i seq (nrow (data)) {idx & lt; - which (data [i, 1: 4] == "99") idxn & lt; - which (data [i, 1: 4] = "99") cols99 & lt; - colnames (data) [IDX] colsn99 & lt; - colnames (data) [idxn] if (any (idx)) {nn & lt; - [paste (c (nn, colsn99), fall = ",") "else {data] - paste (" colnames (data) [idx] "as in '99', data [i," phrase1 "] & lt; - paste [I, "phrase1"] <- paste (colsn99, fall = ",")} if (any (idxn)) {data [i, "phrase2"] & lt; - paste (paste (callon 99) Data (i, "phrase3") & paste; paste (colsn99), paste ("'", data [i, idxn],' ', sep = "", sep = "="), fall = ",") , Collapse = ",")}}
Comments
Post a Comment