2

これは、以前に尋ねたこの質問の続きです: R for loop: create a new column with the count of a sub str from a different column

私は大きなテーブルを持っています(100以上の列、50k以上の行)。列の 1 つには、次の形式のデータが含まれています。

col
chicken
chicken,goat
cow,chicken,goat
cow

私は行きたい:

col         col2         col3
chicken
chicken     goat
cow         chicken      goat
cow

記入する必要がある列は 3 つ以上あります。例としてこれを削除しました。私のスクリプトは、入力する適切な数の列を作成します。コードが必要なだけです。これは for ループであると想定し、「col」の文字列を「,」で分割し、分割された文字列を後続の列に配置します。

助けてくれてありがとう!

4

2 に答える 2

11
read.table(text="chicken
 chicken,goat
 cow,chicken,goat
 cow", fill=TRUE, sep=",")
# Trivial to change the names of dataframe columns
        V1      V2   V3
1  chicken             
2  chicken    goat     
3      cow chicken goat
4      cow       
于 2013-09-18T21:53:45.503 に答える
3

これを試すことができます:

library(splitstackshape)
concat.split(data = df, split.col = 1, sep = ",", drop = TRUE)

#     col_1   col_2 col_3
# 1 chicken              
# 2 chicken    goat      
# 3     cow chicken  goat
# 4     cow 
于 2013-09-18T21:53:06.217 に答える