A5C1D2H2I1M1N2O1R2T1

 GMTs

General chat. Please read the FAQ for the rules or if you want...
Dec 17, 2020 23:47
2013 was when I first started getting into this Winter Bash thing, enough so to ask this question.
Dec 17, 2020 23:45
^^ 2015 was my biggest hat year...
Dec 17, 2020 23:45
Dec 17, 2020 23:43
There's a secret hat for taking the tour, for example.
Dec 17, 2020 23:41
@zx8754 There are a few hats that it seems like we won't be able to get....
Dec 17, 2020 23:40
Some other people got notebooks and stuff that would have been more useful than a ton of stickers.
Dec 17, 2020 23:39
@DavidArenburg I wore my SO t-shirt until it fell apart. Didn't use the stickers though.
Dec 17, 2020 15:33
@DavidArenburg What can I say? I like my virtual hats ;-)
Dec 17, 2020 15:33
@Cath I've got a real quarantine beard. I'm staying at home with the kids, so it's been a good excuse to just do a tidy trim and let it grow out rather than a close trim suitable for an office.
Dec 16, 2020 21:49
Nice ... beard? @Cath ;-p
Dec 16, 2020 21:48
Hello everyone. I hope you're all staying safe.
Dec 16, 2020 21:47
Hats are back! So I must make a temporary return!
 
Dec 3, 2020 23:20
res3 <- c("__label__1 0.500768 __label__2 0.499252",
          "__label__2 0.500768 __label__1 0.499252",
          "__label__3 1")

x <- fread(text = res3, fill = TRUE)
setnames(x, paste0(rep(c("var_", "val_"), length.out = ncol(x)),
                   rep(1:2, each = ncol(x)/2)))[, row := .I][]
out <- melt(x, measure = patterns("var_", "val_"), na.rm = TRUE)[
  , dcast(.SD, row ~ value1, value.var = "value2")]
out
#    row __label__1 __label__2 __label__3
# 1:   1   0.500768   0.499252         NA
Dec 3, 2020 23:20
For example:
Dec 3, 2020 23:20
This should also work if any row has different labels that don't appear in other rows.
Dec 3, 2020 23:16
I've shown what happens in each step, though typically these steps can be combined.
Dec 3, 2020 23:16
x <- fread(text = res2)
x
##            V1       V2         V3       V4
## 1: __label__1 0.500768 __label__2 0.499252
## 2: __label__2 0.500768 __label__1 0.499252
setnames(x, paste0(rep(c("var_", "val_"), length.out = ncol(x)),
                   rep(1:2, each = ncol(x)/2)))[]
##         var_1    val_1      var_2    val_2
## 1: __label__1 0.500768 __label__2 0.499252
## 2: __label__2 0.500768 __label__1 0.499252
x[, row := .I][]
##         var_1    val_1      var_2    val_2 row
## 1: __label__1 0.500768 __label__2 0.499252   1
Dec 3, 2020 23:16
Here's how I'd approach it if the order of labels in each row might be different:
Dec 3, 2020 23:08
@astel, I'll be back in a couple of minutes with an example.
Dec 3, 2020 22:57
To clarify about the label order, do you mean, for example, that row 2 of column V1 could be "__label__2" and V3 could be "__label__1" while row 1 could be in the reverse order?
Dec 3, 2020 22:55
No. It would need to be modified.
Dec 3, 2020 22:55
What OS are you using?
Dec 3, 2020 22:55
I'll go ahead and post it as an answer, though I'm not sure why you weren't able to use fread directly with cmd = .
Dec 3, 2020 22:50
That's strange that you get a data.frame from the output of system. I'm following the code you've posted in your question and I get a character vector.
Dec 3, 2020 22:46
What do you get when you do str(res)?
Dec 3, 2020 22:46
x, as shown above.
Dec 3, 2020 22:43
Since you've gotten to creating res, you could also try x <- fread(text = res).
Dec 3, 2020 22:39
x
#            V1       V2         V3       V4
# 1: __label__1 0.500768 __label__2 0.499252
# 2: __label__1 0.500768 __label__2 0.499252
Dec 3, 2020 22:39
Here, ind is just a way to select every alternate column. The first line would result in something ike:
Dec 3, 2020 22:38
x <- fread(cmd = "fasttext predict-prob model_data.bin data.test.txt 2")
ind <- rep(c(FALSE, TRUE), length.out = ncol(x))
x <- setnames(x[, ..ind], sapply(x[, !ind, with = FALSE], '[[', 1))[]
Dec 3, 2020 22:37
@astel, then the approach I shared should work.
Dec 3, 2020 22:36
What I had in mind with fread would be something like: library(data.table); fread(cmd = "fasttext predict-prob model_data.bin data.test.txt 2"). Looking at the resulting string in your sample data, you'd end up with a 4-column data.table. I don't know enough about the fasttext format to say whether this is a good answer or not. For instance, will there always be the same number of labels per element of res? Will the labels always be the same?
Dec 3, 2020 22:36
For instance, with the reprex you've shared, the following would work: x <- fread(cmd = "fasttext predict-prob model_data.bin data.test.txt 2"); ind <- rep(c(FALSE, TRUE), length.out = ncol(x)); x <- setnames(x[, ..ind], sapply(x[, !ind, with = FALSE], [[, 1))[]; x but I'm not sure whether that solution generalizes to the fasttext data structure in general. Hope this helps!
Dec 3, 2020 22:36
Maybe use intern = TRUE. Have you tried using fread from "data.table" yet?
Dec 3, 2020 22:36
fread (from data.table) should be able to read from system commands. If you dput the head of the data you managed to read in using system and show your desired output, it would be easier to help out.
 
Nov 19, 2020 03:12
Good luck!
Nov 19, 2020 03:12
OK. You'll get much faster help here on Stackoverflow if you share reproducible examples like I've done here in the chat. That's what most of the people answering questions expect.
Nov 19, 2020 03:08
> library(splitstackshape)
> cSplit_e(y, "choice", type="character", fill = 0)
  id                choice choice_that choice_these choice_this choice_those
1  a       this,that,those           1            0           1            1
2  b            that,those           1            0           0            1
3  c            this,those           0            0           1            1
4  d this,that,those,these           1            1           1            1
Nov 19, 2020 03:08
If your data's like that, you can use my splitstackshape package to get the binary data:
Nov 19, 2020 03:06
And from there, they're creating a binary matrix.
Nov 19, 2020 03:06
> y <- data.frame(id = c("a", "b", "c", "d"), choice = c("this,that,those", "that,those", "this,those", "this,that,those,these"))
> y
  id                choice
1  a       this,that,those
2  b            that,those
3  c            this,those
4  d this,that,those,these
Nov 19, 2020 03:06
I'm guessing that the researchgate data looks like this:
Nov 19, 2020 03:03
The researchgate link you've shared describes what sounds like a very different data structure.
Nov 19, 2020 03:03
> (!is.na(x[2:5]))+0
     choice1 choice2 choice3 choice4
[1,]       1       1       1       0
[2,]       0       1       1       0
[3,]       1       0       1       0
[4,]       1       1       1       1
Nov 19, 2020 03:03
> x
  id choice1 choice2 choice3 choice4
1  a    this    that   those    <NA>
2  b    <NA>    that   those    <NA>
3  c    this    <NA>   those    <NA>
4  d    this    that   those   these
Nov 19, 2020 03:01
x <- data.frame(id = c("a", "b", "c", "d"), choice1 = c("this", NA, "this", "this"), choice2 = c("that", "that", NA, "that"), choice3 = c("those", "those", "those", "those"), choice4 = c(NA, NA, NA, "these"))
Nov 19, 2020 03:01
Here's an example:
Nov 19, 2020 03:01
@Pre, one way would be to do something like (!is.na(x[cols_of_interest]))+0 (where cols_of_interest would be the columns you want to convert to binary.
Nov 19, 2020 02:59
@Pre, What would be an example of a 2-way frequency table's input and output from your data? The "questionr" package also has a cross.multi.table function that you might want to look at.
Nov 19, 2020 02:59
For the column names, if you are sure there would be at least one non-NA value in each column, recreating names shouldn't be tough...