Reputation: 37
I need help with R in finding the right way to apply the same process as the unique()
function does, just in the whole table. My data are organized by rows, with no column names. It is a big table, 130 X 180. So, what I would need is a list of all unique elements by row, and information on how many times each of the elements appears in each row.
example of my data:
Row1, F, M2, E, E, H, E, E, H, E, E, M, 21, E, M, L, L, L, L, L, E, H, L, L, L, L, L, L, L, L, L, L, E, H, L, L, L, L, L, L, L, L, L, E, H, L, L, L, L, L, L, L, L, L
Row2, A3, A3, V, R, A3, A3, V, R, A3, A3, V, R, A3, A3, R, A3, A3, V, R, A3, A3, V, R, A3, A3, V, V, W, 12, N, N, N, W, N, N, N, 21, N, N, W, N, N, N, 21, N, N, W
Row3, I, M, A1, A1, H, A1, A1, H, A1, A1, H, A1, A1, H, M, D2, M, L, L, L, L, D2, M, L, L, L, L, D2, M2, G, M2, G, M2, G, R, K, E, R, K, E, R, K, E, R, K, E
Row4, H, A1, A1, H, A1, A1, H, A1, A1, M, A1, A1, H, A1, A1, A1, W, N, N, 21, N, N, W, N, 21, W, 21, W, W, Q, Q, Q, Q, Q, Q, L, F, D, Q, Q, Q, Q, Q, F, D, Q
Which can be found as a .txt file here
The correct answer for Row1
would be (plus the frequency of elements, which I don't know how to count):
> unique(Row1)
[1] " F" " M2" " E" " H" " M" " 21" " L"
But when I try to apply it to the whole table, it counts by columns, and I need the answer by rows.
Upvotes: 1
Views: 429
Reputation: 887118
We can use apply
to loop over the data and get the unique
apply(df1[-1], 1, unique)
If there are leading, lagging spaces, use trimws
to remove those
apply(df1[-1], 1, function(x) unique(trimws(x)))
#[[1]]
#[1] "F" "M2" "E" "H" "M" "21" "L"
#[[2]]
#[1] "A3" "V" "R" "W" "12" "N" "21" ""
#[[3]]
# [1] "I" "M" "A1" "H" "D2" "L" "M2" "G" "R" "K" "E" ""
#[[4]]
# [1] "H" "A1" "M" "W" "N" "21" "Q" "L" "F" "D" ""
If it needs the frequency, use table
apply(df1[-1], 1, table)
df1 <- read.csv("Data_exmpl.txt", fill = TRUE, header = FALSE)
Upvotes: 4