Strings in rows in R -
i have file , made function in r accepts string 110110110110110.
now, input file has 1000's of rows , each row has these numbers in subsequent columns.
example:
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 1 1 1 0 1 1 0 1 1 0 1 1 0 1 1 2 0 1 0 1 0 1 0 1 0 1 0 1 0 1 3 4 5 6
like this, have thousand's of rows. column v1 row number , v2-v28 contains either 1 or 0. now, want apply function every row of input file such numbers in columns v2-v28 forms input string. know how if had simple strings in rows in file, how columns?
thanks.
edit: function have written accepts string input such "110110110". however, have sequence of 1's , 0's in different columns (v2-v28) in each row.
like this?
df <- data.frame(v1 = c(1,0,0,0,1), v2= c(0,1,0,1,0), v3=c(0,0,0,0,0), v4=c(1,1,1,1,1)) input <- do.call(cbind, df[,2:ncol(df)])
now can utilize apply margin 1 rows, pass specified function:
apply(input, 1, do.something.with.vector)
edit: convert string , remove commata etc utilize this
apply(input, 1, function(x) print(gsub(", ","",tostring(x))))
you can exchange "print" "do.something.with.vector" function.
r
No comments:
Post a Comment